2013-02-04 17 views
0

我的C#Web應用程序駐留在C驅動器上。 但是,應用程序接收用戶上傳的文檔並將其保存在D驅動器上。從C中的不同驅動器檢索文檔#

如何在C驅動器上的Web應用程序的HyperLink控件的NavigateUrl屬性中指定D驅動器上文檔的路徑。

回答

0

在服務器端,你可以在此文件對象加載Stream和響應裏面的byte[]

public void Page_Load(object sender, System.EventArgs e) 
{ 
    // create a FileStream from a path from local (D:, C:, E:, etc...) 
    FileStream file = File.OpenRead(@"d:\folder\yourfile.txt"); 

    //Convert the stream to an array of bytes. 
    byte[] byteArray = file.ToArray(); 

    // Clear all content output from the buffer stream 
    Response.Clear(); 

    // Add a HTTP header to the output stream that specifies the default filename 
    // for the browser's download dialog 
    Response.AddHeader("Content-Disposition", "attachment; filename=foo.txt"); 

    // Add a HTTP header to the output stream that contains the 
    // content length(File Size). This lets the browser know how much data is being transfered 
    Response.AddHeader("Content-Length", byteArray.Length.ToString()); 

    // Set the HTTP MIME type of the output stream 
    Response.ContentType = "application/octet-stream"; 

    // Write the data out to the client. 
    Response.BinaryWrite(byteArray); 
} 
+0

非常感謝費利佩。我需要的是如何指定超鏈接的NavigateUrl屬性以導航到D:驅動器上的文檔。 – AbdulQadir

+0

你不能這樣做,因爲你將在服務器端執行它,NavigateUrl重定向到一個url,並且在這個過程中,從wwww文件夾外的另一個驅動器或文件夾獲取文件並響應客戶端。您可以在url中傳遞參數並使用queystring處理它。 –