Different name for GroupDocs temp folder

Hello Dhivya,


Sorry for the delay and thank you for the description. In such case the only way that you can achieve your goal is to use stream in the Viewer.

For example you can try to use such logic:
1. In the Globals use such initialization code:
Viewer.SetRootStoragePath(Server.MapPath("~/App_Data"), Server.MapPath("~/App_Data/Cache"));
2. In the controller of the web page with Viewer (for example lets say it will be Index action):
public ActionResult Index()
{
const string testFilesDir = “~/testfiles/”;
string[] filePaths ={
@Server.MapPath(testFilesDir + “candy.pdf”),
};

StreamDefinition [] streamDefinitions = filePaths.Select(path => new StreamDefinition
{
Stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read),
FilenameExtension = Path.GetExtension(path)
}).ToArray();
return View(streamDefinitions);
}
3. In the view file:
@model StreamDefinition[]
@{
StreamDefinition[] streamDefinitions = Model;
}

@(Html.ViewerClientCode()
.TargetElementSelector("#test2")
.Streams(Model, “candy_timesheet”)
.OpenThumbnails()
.ZoomToFitWidth()
.BackgroundColor("#777")
.Locale(“es-ES”)
.ShowHeader(false)
.UseHtmlBasedEngine(false)
.UseInnerThumbnails()
)
@{
foreach (StreamDefinition streamDefinition in streamDefinitions)
{
streamDefinition.Stream.Dispose();
}
}

Please note that const string testFilesDir should be the path that you will use currently.
With such logic your App_Data folder will not contain any files that you view only the cache.

Also please find attached example code source.

Best regards.

Hi,


We have thousands of documents and we open the preview and loads it only when the user clicks on the link after doing some other operations in jquery. So we right now have the below code to initialise the viewer. How do I return the StreamDefinition from my controller to my jquery in ajax call?

$(tabName).groupdocsViewer({
filePath: data.fileName,
docViewerId: ‘doc_viewer’ + tabName,
PreloadPagesCount: 1,
localizedStrings: localizedStrings, thumbsImageBase64Encoded: thumbsImageBase64Encoded,
quality: 100, showThumbnails: true, openThumbnails: false, initialZoom: 100,
zoomToFitWidth: true, onlyShrinkLargePages: false, zoomToFitHeight: false, width: 0, height: 0, backgroundColor: null,
showFolderBrowser: false, showPrint: true, showDownload: true, showZoom: true, showPaging: true, showViewerStyleControl: true,
showSearch: true, preloadPagesOnBrowserSide: false, convertWordDocumentsCompletely: false, viewerStyle: 1,
supportTextSelection: true, usePdfPrinting: false, toolbarButtonsBoxShadowStyle: null, toolbarButtonsBoxShadowHoverStyle: null,
thumbnailsContainerBackgroundColor: null, thumbnailsContainerBorderRightColor: null, toolbarBorderBottomColor: null, toolbarInputFieldBorderColor: null,
toolbarButtonBorderColor: null, toolbarButtonBorderHoverColor: null, thumbnailsContainerWidth: 0, jqueryFileDownloadCookieName: ‘jqueryFileDownloadJSForGD’,
showDownloadErrorsInPopup: false, showImageWidth: false, showHeader: true, minimumImageWidth: 0, enableStandardErrorHandling: true, useHtmlBasedEngine: true,
useHtmlThumbnails: false, useImageBasedPrinting: true, fileDisplayName: docDesc + ‘.’ + docExtn, downloadPdfFile: false, searchForSeparateWords: false, preventTouchEventsBubbling: false,
useInnerThumbnails: false, watermarkText: null, watermarkColor: null, watermarkPosition: ‘Diagonal’, watermarkFontSize: 0, printWithWatermark: false, supportPageReordering: false,
searchHighlightColor: null, currentSearchHighlightColor: null, treatPhrasesInDoubleQuotesAsExactPhrases: false, usePngImagesForHtmlBasedEngine: false, showOnePageInRow: false, loadAllPagesOnSearch: false,
useEmScaling: false, ignoreDocumentAbsence: false, supportPageRotation: false, useRtl: false, useAccentInsensitiveSearch: false, useVirtualScrolling: false, supportListOfContentControls: false, supportListOfBookmarks: false,
embedImagesIntoHtmlForWordFiles: false
});

Thanks,
Dhivya

This is my angularjs call and I am not able to receive the streamdefinition here.



$http({
method: “POST”,
url: myaction,
data: docPath,
}).success(function (data, status, headers, config) {
}
Please let me know what should be my responseType to get StreamDefinition as return value.

Thanks,
Dhivya

Hello Dhivya,

Thank you for the question. In such case you need to update your code with the following:

  1. In the view change your AJAX call in such a way:
<div id="container"></div>
<div id="test" style="margin-bottom:20px"></div>
$(document).ready(function () {
    var serviceURL = "myaction";

    $.ajax({
        type: "POST",
        url: serviceURL,
        data: docPath,
        success: successFunc,
        error: errorFunc
    });

    function successFunc(data, status) {
        $("#container").append(
            '<div id="test" style="width: 100%; margin-bottom: 20px; height: 900px;" runat="server"></div>' +
            '</div>' + data
        );
    }

    function errorFunc() {
        alert('error');
    }
});
  1. In the controller (assuming the action is named Ajax()):
public ActionResult Ajax()
{
    string filename = "candy.pdf";
    // Obtain a stream from wherever you need. Here we get it from a file.
    string path = Server.MapPath("~/testfiles/" + filename);
    System.IO.Stream byteStream = new System.IO.FileStream(
        path,
        System.IO.FileMode.Open,
        System.IO.FileAccess.Read
    );

    string groupdocsViewerScript = Groupdocs.Web.UI.Viewer.ClientCode()
        .TargetElementSelector("`#test`")
        .Stream(byteStream, filename, "pdf")
        .Height(900)
        .ToString();

    return Content(groupdocsViewerScript, "text/plain");
}

That’s all.

In few words, you just need to follow this logic:

  1. From the view you make an AJAX call to an action, sending the file name in the data.
  2. In the action you receive the file name and obtain its stream.
  3. In the same action you generate the viewer code (e.g., see the example Viewer code) with this stream (adjust options as needed).
  4. Return the generated viewer code as a string.
  5. In the success function of the AJAX call, add the HTML content to the page. Note that you need two <div> elements for the viewer: one with id="container" (the main div) and one with id="test" (where the viewer script will be injected).

Best regards.

Hi,

I have couple of questions.

1. Does the viewer really needs to copy the file in the form of stream for generating the preview? Isn't the path alone enough? Some of the files are very large and takes forever to get the filestream.


Thanks,
Dhivya

Hi Dhivya,

Thank you for these questions.

  1. In your use case using of stream is an only way to achieve your goal – to store cache in one place inside the application.

  2. Full code of the widget will be:

string groupdocsViewerScript = Groupdocs.Web.UI.Viewer.ClientCode()
    .TargetElementSelector("#test")
    .Stream(ByteStream, filename, "pdf")
    .PreloadPagesCount(1)
    .Quality(100)
    .ShowThumbnails(true)
    .OpenThumbnails(false)
    .Zoom(100)
    .ZoomToFitWidth(true)
    .Width(0)
    .ShowFolderBrowser(false)
    .SupportTextSelection(true)
    .UsePdfPrinting(false)
    .ShowDownloadErrorsInPopup(false)
    .ShowImageWidth(false)
    .EnableStandardErrorHandling(true)
    .UseHtmlBasedEngine(true)
    .DownloadPdfFileIfPossible(false)
    .SearchForSeparateWords(false)
    .ShowHeader(true)
    .Height(0)
    .ToString();

All other options that you have are set to default values, so you don’t need to set them here. If you need to change some other option value, you can find all options as shown in the screenshot.

Best regards.

Hi again,


To clear the cache folder you should use such code:
DocumentCache documentCache = new DocumentCache(Server.MapPath("~/GroupDocs.Viewer.for.NET.lic"), Server.MapPath("~/App_Data"));
TimeSpan timeSpan = new TimeSpan(0, 30, 0);
documentCache.RemoveOldEntries(timeSpan);

It will delete all files which older then 30 minutes.

Thank you

Hi,


Thanks for the solution it worked.

Regarding the first answer, are you saying there will be a performance hit if we use stream instead of the other way?

Thanks,
Dhivya

Hi,


If you will use stream you will not get performance issues - all will work with same performance as in a common way. You must use stream only because the temp files will be stored in one place in such case.

Thank you.

Perfect!! Thanks a lot. Everything is working now as required.


Thanks,
Dhivya

Hi


Glad to hear that.

Best regards.

Hi,


I have this new issue when I try to view an excel sheet with quotes as the Sheet name. The error is coming as Uncaught SyntaxError: Unexpected string. Tried attaching the excel but the forum is not allowing me to attach the excel files. The sheet name is test !""£$

Please help.

Thanks,
Dhivya

Hi,


The issue reason is that that such symbols are restricted by the web server. Please avoid using such symbols for the folder or file names

Best regards.

Hi,


The issue is its not the file or folder name but its the excel’s one of the worksheet name. We do have some documents where the sheet name needs to have “”. Can this be handled?

Thanks,
Dhivya

Hi,


Sorry but this issue is a common web issue which doesn’t related with our Viewer.
Please rename such sheet names.

Best regards.