2011-07-07 198 views

回答

4

輸出到文件的直接鏈接的問題是,對於某些內容類型,它可能只是在瀏覽器窗口中打開。如果這不是理想的結果,並且您想強制保存文件對話框,則需要編寫一個ASP/PHP頁面,您可以通過查詢字符串傳遞文件名。然後該頁面可以讀取該文件並在響應上設置一些標題以指示內容處置是和附件。

對於ASP.net,如果創建一個名爲download.aspx一個簡單的aspx頁面,添加以下代碼,然後把這個文件在服務器上的某個地方,你可以通過調用這個頁面這樣的下載文件:

http://yourserveraddress/download.aspx?path=http://yoursharepointserver/pathtodoclibrary/file.ext

protected void Page_Load(object sender, EventArgs e) 
    { 
     string path = ""; 
     string fileName = ""; 

     path = Request.QueryString["path"]; 
     if (path != null && path.Length > 0) 
     { 
      int lastIndex = path.LastIndexOf("/"); 
      fileName = path.Substring(lastIndex + 1, (path.Length - lastIndex - 1)); 

      byte[] data; 
      data = GetDataFromURL(path); 

      Response.Clear(); 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
      Response.BinaryWrite(data); 
      Response.Flush(); 
     } 
    } 


    protected byte[] GetDataFromURL(string url) 
    { 
     WebRequest request = WebRequest.Create(url); 
     byte[] result; 
     byte[] buffer = new byte[4096]; 

     //uncomment this line if you need to be authenticated to get to the files on SP 
     //request.Credentials = new NetworkCredential("username", "password", "domain"); 

     using (WebResponse response = request.GetResponse()) 
     { 
      using (Stream responseStream = response.GetResponseStream()) 
      { 
       using (MemoryStream ms = new MemoryStream()) 
       { 
        int count = 0; 
        do 
        { 
         count = responseStream.Read(buffer, 0, buffer.Length); 
         ms.Write(buffer, 0, count); 
        } while (count != 0); 
        result = ms.ToArray(); 
       } 
      } 
     } 
     return result; 
    } 
0

我想創建一個LinkBut​​ton和設置的URL文檔的URL編程。

+0

yes我正在嘗試這個..在鏈接按鈕單擊我傳遞文檔id和使用id獲取文檔的url。 謝謝。 :) – Rushikesh

相關問題