如何在 ASP.NET MVC 项目中使用 GroupDocs Viewer for .NET?
How to use GroupDocs Viewer for .NET in an ASP.NET MVC Project?
我在我的项目中安装了 GroupDocs Viewer。
在全球范围内
Groupdocs.Web.UI.Viewer.InitRoutes();
Groupdocs.Web.UI.Viewer.SetRootStoragePath(Server.MapPath("~/DietTemplate/"));
并在控制器中
var ByteStream = new FileStream(Server.MapPath("~/DietTemplate/L0.docx"),
FileMode.Open, FileAccess.Read, FileShare.Read);
//ViewData
ViewData["stream"] = ByteStream;
ViewData["filename"] = "L0.docx";
ViewData["extension"] = "docx";
ViewData["fileDisplayName"] = "Defined document.pdf";
return View();
在视野中
@section Styles{
@Html.CreateViewerScriptLoadBlock().LoadJquery().LoadJqueryUi()
}
@(Html.ViewerClientCode()
.TargetElementSelector("#test")
.Stream(ViewBag.Stream,
ViewBag.Filename,
ViewBag.Extension,
ViewBag.FileDisplayName)
.ZoomToFitWidth()
)
编辑
在视图中添加脚本
<script type="text/javascript"> $(function () { var localizedStrings = null;var thumbsImageBase64Encoded = null;$('#test').groupdocsViewer({ localizedStrings: localizedStrings, thumbsImageBase64Encoded: thumbsImageBase64Encoded, filePath: 'temp\S\L0.docx',quality: 100,showThumbnails: true,openThumbnails: true,initialZoom: 100,zoomToFitWidth: true,onlyShrinkLargePages: false,zoomToFitHeight: false,width: 0,height: 0,backgroundColor: null,showFolderBrowser: true,showPrint: true,showDownload: true,showZoom: true,showPaging: true,showViewerStyleControl: true,showSearch: true,preloadPagesCount: null,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: false,useHtmlThumbnails: false,useImageBasedPrinting: true,fileDisplayName: 'Defined document.pdf',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,useVirtualMode: false,supportListOfContentControls: false,supportListOfBookmarks: false,embedImagesIntoHtmlForWordFiles: false}); }); </script>
但不显示任何内容。 :(
从您发布的代码中,我可以看到您在控制器端使用 ViewData
字典,在视图端使用 ViewBag
字典。而您应该使用 ViewData
或 ViewBug
。它们不能同时使用。
如果您想使用 ViewData
,则在视图上代替:
.Stream(ViewBag.Stream, ViewBag.Filename, ViewBag.Extension, ViewBag.FileDisplayName)
你应该有这些:
.Stream((Stream)ViewData["stream"], (String)ViewData["filename"], (String)ViewData["extension"], (String)ViewData["fileDisplayName"])
P.S。我在 GroupDocs 担任开发人员布道师。
我在我的项目中安装了 GroupDocs Viewer。
在全球范围内
Groupdocs.Web.UI.Viewer.InitRoutes();
Groupdocs.Web.UI.Viewer.SetRootStoragePath(Server.MapPath("~/DietTemplate/"));
并在控制器中
var ByteStream = new FileStream(Server.MapPath("~/DietTemplate/L0.docx"),
FileMode.Open, FileAccess.Read, FileShare.Read);
//ViewData
ViewData["stream"] = ByteStream;
ViewData["filename"] = "L0.docx";
ViewData["extension"] = "docx";
ViewData["fileDisplayName"] = "Defined document.pdf";
return View();
在视野中
@section Styles{
@Html.CreateViewerScriptLoadBlock().LoadJquery().LoadJqueryUi()
}
@(Html.ViewerClientCode()
.TargetElementSelector("#test")
.Stream(ViewBag.Stream,
ViewBag.Filename,
ViewBag.Extension,
ViewBag.FileDisplayName)
.ZoomToFitWidth()
)
编辑
在视图中添加脚本
<script type="text/javascript"> $(function () { var localizedStrings = null;var thumbsImageBase64Encoded = null;$('#test').groupdocsViewer({ localizedStrings: localizedStrings, thumbsImageBase64Encoded: thumbsImageBase64Encoded, filePath: 'temp\S\L0.docx',quality: 100,showThumbnails: true,openThumbnails: true,initialZoom: 100,zoomToFitWidth: true,onlyShrinkLargePages: false,zoomToFitHeight: false,width: 0,height: 0,backgroundColor: null,showFolderBrowser: true,showPrint: true,showDownload: true,showZoom: true,showPaging: true,showViewerStyleControl: true,showSearch: true,preloadPagesCount: null,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: false,useHtmlThumbnails: false,useImageBasedPrinting: true,fileDisplayName: 'Defined document.pdf',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,useVirtualMode: false,supportListOfContentControls: false,supportListOfBookmarks: false,embedImagesIntoHtmlForWordFiles: false}); }); </script>
但不显示任何内容。 :(
从您发布的代码中,我可以看到您在控制器端使用 ViewData
字典,在视图端使用 ViewBag
字典。而您应该使用 ViewData
或 ViewBug
。它们不能同时使用。
如果您想使用 ViewData
,则在视图上代替:
.Stream(ViewBag.Stream, ViewBag.Filename, ViewBag.Extension, ViewBag.FileDisplayName)
你应该有这些:
.Stream((Stream)ViewData["stream"], (String)ViewData["filename"], (String)ViewData["extension"], (String)ViewData["fileDisplayName"])
P.S。我在 GroupDocs 担任开发人员布道师。