2010-11-16 74 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