【问题标题】:System.Diagnostics.Process.Start(OutputFileName) works on local machine but not in webserver - vb.netSystem.Diagnostics.Process.Start(OutputFileName) 在本地机器上工作,但在网络服务器上不工作 - vb.net
【发布时间】:2014-11-16 05:25:29
【问题描述】:

亲爱的,我在 vb.net 和 asp.net 应用程序中创建了一个函数

(1)- 从前端,用户选择一个页面 (list.aspx) 中列出的多个文件名(通过复选框),然后单击导出按钮以在一个 pdf 中查看所有文档文件 (2)- 系统从数据库中抓取这些文档,(3)- 系统将它们转换为 pdf 文件格式和 (4)- 系统将这些 pdf 文件合并为一个 pdf 文件,然后 (5)- 系统将其显示在 iframe 中。

从 list.aspx 页面,当有人点击导出 cv 时,它会执行此代码

window.location.href = 'export-cvs.aspx?v=' + vid + '&a=' + appids.toString();

在 export-cvs.aspx 页面中执行以下代码

<object id="cvFrame" width="100%" height="500px" type="application/pdf"  data="preview-bulk-cv.ashx?v=<%= Vacancy.ID%>&a=<%= Request("a") %>"></object>

主要代码在preview-bulk-cv.ashx页面,如下。

Public Class PDFMerge : Implements IHttpHandler

Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest


Dim vacancy = New Vacancy(context.Request("v"))
Dim sourceFiles = New List(Of String)()


For Each docPath As String In From row As DataRow In DB.GetData("query").Rows Select HttpContext.Current.Server.MapPath("~/documents") & "\" & Left(guid, 1) & "\" & Right(guid, 1) & "\" & guid & "." & System.IO.Path.GetExtension(row.Item("originalfilename")).ToLower().Substring(1)

   cnt+=1

    Dim epath As String = HttpContext.Current.Server.MapPath("~/Downloads") & "\"  & Now.ToString("yyyy-MMM-dd-HHmmss") & "_" & cnt.Tostring & ".pdf"

    Converter.ConvertDocument(docPath, epath)
    If File.Exists(epath) Then
        sourceFiles.Add(epath)
    End If

Next

Dim OutputFileName As String = HttpContext.Current.Server.MapPath("~/Downloads") & "\" & vacancy.Title.Replace(" ", "_") & ".pdf"

PDFMerge.MergeFiles(OutputFileName, sourceFiles.ToArray)
Dim mPDFFile As FileStream = File.OpenRead(OutputFileName)
Dim mPDFFileBuffer(mPDFFile.Length - 1) As Byte
mPDFFile.Read(mPDFFileBuffer, 0, mPDFFileBuffer.Length)
mPDFFile.Close()

System.Diagnostics.Process.Start(OutputFileName)


context.Response.Clear()
context.Response.ContentType = "application/pdf"
context.Response.AddHeader("Content-Disposition", "attachment;filename=" & OutputFileName)
context.Response.AddHeader("Content-Length", mPDFFileBuffer.Length)
context.Response.OutputStream.Write(mPDFFileBuffer, 0, mPDFFileBuffer.Length)
mPDFFileBuffer = Nothing
context.Response.Flush()
context.Response.End()



End Sub
End Class    

你可以在我上面的代码中看到。我使用以下行打开了最终的 pdf 文档。

System.Diagnostics.Process.Start(OutputFileName)

此代码在本地服务器中有效,但在网络服务器中无效。即当我在本地主机上运行应用程序时打开 pdf 文件。但是当应用程序存储在网络服务器上并且网络应用程序作为网络应用程序从任何机器运行时,pdf文件不会打开。我已经在网络服务器中检查了文件正在正确合并到正确的文件夹中。

网络服务器是用 windows 构建的。它的 windows 版本 2008 r2 和 IIS 版本 7.5

我怎样才能让它工作?

【问题讨论】:

  • 您希望 PDF 在您的客户端计算机上打开吗?该代码将尝试在服务器上运行,而使用该网站的人将永远看不到它。
  • 是的,我希望在客户端机器上打开文件。您能否提供将在客户端计算机上打开 pdf 的代码。我可以在 export-cv.aspx 页面的 iframe 上显示 pdf 文件吗?你能建议我怎么做吗?

标签: asp.net vb.net pdf pdf-generation


【解决方案1】:

我知道这个例子是 C#,但这里有一个链接演示了将 PDF 流式传输到浏览器。 http://support.microsoft.com/kb/306654

您还可以调查 Response.WriteFile() 以查看它是否满足您的需求。

【讨论】:

  • Response.WriteFile() 正在工作。虽然 pdf 没有在 iframe 中显示,但现在正在下载。
猜你喜欢
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多