【问题标题】:Display document saved in blob format显示以 blob 格式保存的文档
【发布时间】:2020-08-02 20:08:37
【问题描述】:

我必须以 varbinary(max) 格式显示保存在 SQL Server 上的文档。在以前的页面版本中,页面中显示的是单个文档,在 Page_Load 方法中调用了检索文档并将其显示在页面上的方法,并且它起作用了。

protected void Page_Load(object sender, EventArgs e) {
  ...
  int codeSiteId = -1;
  long docId = -1;
  if (!string.IsNullOrEmpty(qs["codesiteid"]))
    {
        if (!Int32.TryParse(qs["codesiteid"], out codeSiteId))
        {
            return;
        }
    }
    //else return;


    if (!string.IsNullOrEmpty(qs["docid"]))
    {
        if (!Int64.TryParse(qs["docid"], out docId))
        {
            return;
        }
    }
  ShowDocumentModel(docId, codeSiteId);
  ...
}

protected void ShowDocumentModel(long documentModelId, int codesiteid)
{
    string temppath = helpsi.framework.core.Configurator.Instance.getAppSettingsValue("APP_REGISTRATION_TEMP_PATH", codesiteid);
    string filename="";
    string pdffile = string.Empty;
    try
    {
        documentLoading.Visible = true;
        string path = Path.GetFullPath(Server.MapPath(temppath));
        helpsi.framework.core.CustomerProvider.DocumentModelResult result = helpsi.framework.core.CustomerProvider.DocumentModel.GetDocumentModelContent(documentModelId);
        if (!string.IsNullOrEmpty(result.FileName))
        {
            string unescapeFileName =  HttpUtility.UrlDecode(result.FileName);
            /*unescapeFileName = unescapeFileName.Replace("<", "").Replace(">", "").Replace("+", "").Replace("{", "").Replace("}", "").Replace("/", "");
     */

            string[] blackList = {"<", ">", "\\+", "{", "}", "/","\\*", ":" };
            for (int i = 0; i < blackList.Length; i++)
            {
                unescapeFileName = System.Text.RegularExpressions.Regex.Replace(unescapeFileName, blackList[i], "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            }

            filename = Path.Combine(path, unescapeFileName.Replace(" ", "").ToLower());
            File.WriteAllBytes(filename, result.Content);
            descDocumentModel.InnerText = result.DescDocumentModel;
            descDocumentModelState.Attributes.Add("i18n", result.DocumentModelState);
            descDocumentModelType.Attributes.Add("i18n", result.DocumentModelType);
            documentModelUploadDate.InnerText = PageUtil.jsonString(result.UploadDate.ToString("s"), HttpContext.Current);
            switch (result.ContentType.Split('/')[0])
            {
                case "image":
                    hfTypeFile.Value = "image";
                    hfImageFile.Value = Page.ResolveUrl(Path.Combine(temppath, Path.GetFileName(filename))); ;
                    break;
                default:
                    string ext = Path.GetExtension(filename);                        
                    if (ext.Equals(".rtf", StringComparison.CurrentCultureIgnoreCase) || ext.Equals(".doc", StringComparison.CurrentCultureIgnoreCase) || ext.Equals(".docx", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (!PdfConverter.ConvertDocToPdf(filename, Server.MapPath(temppath), codesiteid, out pdffile))
                            pdffile = Path.GetFileName(filename);
                    }
                    else
                    {
                        pdffile = Path.GetFileName(filename);
                    }
                    pdffile = Page.ResolveUrl(Path.Combine(temppath, Path.GetFileName(pdffile)));
                    previewFile.Attributes["src"] = pdffile;
                    previewFile.Visible = true;
                    break;
            }
        }
        hfdocumentModelStateId.Value = helpsi.framework.core.CustomerProvider.DocumentModel.get<helpsi.framework.core.CustomerProvider.DocumentModel>(documentModelId).CODE_DOCUMENT_MODEL_STATE_ID.ToString();
        hfdocumentModelId.Value = documentModelId.ToString();            
    }
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
        documentLoading.Visible = false;
    }
}

页面中有一个iframe来显示文档预览

    <div class="ui-layout-center">
    <div class="wrapper" runat="server" id="content">
        <div id="viewerDocs" style="height:100%;width:100%">
            <img id="documentLoading" src="../images/loadsmall.gif" border="0" runat="server"  visible="false"/>
            <iframe id="previewFile" src="about:blank" width="100%" height="551" frameborder="0" runat="server" visible="true"></iframe> 
        </div>
        <div class="imgViewver">
            <div id="viewer" class="viewer"></div>
        </div>
    </div>
</div> 

现在页面变了,因为不再是单个文档,而是多了个文档,这些文档的详细信息以网格的形式显示。加载页面时不显示文档预览,但是单击网格元素时,文档预览必须显示在 iframe 中。有一种方法可以通过 jQuery 执行此操作,也许可以在 ajax 调用中转换 ShowDocumentModel?谢谢

【问题讨论】:

    标签: c# jquery asp.net ajax blob


    【解决方案1】:

    我找到了一个解决方案,我将文件内容保存在一个临时文件中,并在 ajax 调用中返回一个 JSON 对象,其中包含保存的临时文件的 URL。所以在 javascript 文件中,我使用返回的 URL 来填充 iframe 的 src 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2021-04-09
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多