2010-11-16 68 views
0

在我们的应用程序中,我们允许用户上传可以是PDF,Doc,XLS,TXT的文档。上传的文件将被保存在网络服务器上。我们需要为每个上传的文档用户显示链接,当用户点击该链接时,应打开相关文档。预计需要软件来打开相关文件。使用asp.net查看文件

要上传文件,我们使用FileUpload控件的saveAs方法,它工作得很好。 现在,如何查看它?

我相信,我需要将文件复制/下载到本地用户机器,并需要使用Process.Start打开它。

为此,我需要找到用户本地临时目录。如果我把path.GetTempPath(),它给我的Web服务器目录和复制文件。

File.Copy(
    sPath + dataReader["url"].ToString(), 
    Path.GetTempPath() + dataReader["url"].ToString(), 
    true); 

请指教。

回答

0

你不能只在页面上指向文件所在目录的链接吗? 例如 <a href=downloadedfiles/filename.pdf> click here </a>

1

您无法从网络服务器写入用户驱动器。

可以做只是提供一个链接,将文件下载到客户端。 将Content-Disposition标题设置为“附件”以使“保存为”对话框出现,或者“内联”以使其通过客户端的注册程序显示在浏览器中。

您可以创建一个包含这样的代码在服务器端处理程序LinkButton

byte[] data = ...; // get the data from database or whatever 

Response.Clear(); // no contents of the aspx file needed 

Response.CacheControl = "private"; 
Response.ContentType = "application/pdf"; // or whatever the mimetype of your file is 
Response.AppendHeader("Content-Disposition", "attachment;filename=statistic.pdf"); 
Response.AppendHeader("Content-Length", data.Length.ToString(CultureInfo.InvariantCulture)); 

Response.BinaryWrite(data); 

Response.End(); // no further processing of the page needed 
0

一旦你提供的链接,你的工作就完成了。大多。如果客户端的浏览器能够处理基于文件扩展名的文件类型,则它将在单击链接时处理加载文件。

我更喜欢使用http处理程序来引用网页上的文件链接。这对于您需要实施上传文件访问安全的那一天非常重要;否则,任何用户都可以访问任何文件。

0

您不必将文件下载到用户机器。

// For pdf documents 
Response.Clear(); 
string filePath = "File path on the web server"; 
Response.ContentType = "application/pdf";  // for pdf 
Response.WriteFile(filePath); 

// For word documents 
Response.Clear(); 
string filePath = "File path on the web server"; 
Response.ContentType = "application/msword"; 
Response.WriteFile(filePath); 

// similarly other file types