0

我需要幫助以編程方式打開在線Sharepoint文件。這些文件需要在Sharepoint上保護(以便用戶不能漫步),但我也需要使用我的web部件(它將檢查安全性)以某種方式訪問​​它。通常情況下,您只需使用鏈接到Sharepoint文件的超鏈接,但我們不希望人們共享這些超鏈接,這樣就無法工作。以編程方式使用c打開安全Sharepoint文件#

我希望用戶點擊一個鏈接,讓他們看到該文件的「打開,保存,關閉」對話框菜單。

我已經試過這

  String fileName = @"file.txt"; 
      String filePath = @"~online filePath/"; 

      System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
      response.ClearHeaders(); 
      response.ClearContent(); 
      response.Clear(); 
      response.ContentType = "text/plain"; 
      //or whatever file type, I can code that later 
      response.AddHeader("Content-Disposition", "attachment; Filename=" + fileName + ";"); 
      response.TransmitFile(filePath + fileName); 
      //response.Flush(); 
      response.End(); 

不幸的是,雖然這對於本地文件(C:// ...)正常工作,當我嘗試這在我的網頁FIKE,我只收到一個文本文件,擁有當前頁面的源代碼。不知道爲什麼。

至於處理權限,我打算使用提升權限讓程序能夠訪問文件。

我將不勝感激任何幫助,謝謝。

回答

0

下面是示例代碼來提供文件下載與SharePoint

using (SPWeb web = SPContext.Current.Web) 
{ 
    //Get fileName and filePath here 
    response.AddHeader("Content-disposition", "attachment; filename=" + fileName); 
    response.ContentType = "application/octet-stream"; 
    SPFile file = web.GetFile(filePath); 
    byte[] fileBytes = file.OpenBinary(); 
    response.OutputStream.Write(fileBytes, 0, fileBytes.Length); 
    response.End(); 
} 

這是否幫助或你有定位的文件名和文件路徑問題?

+0

謝謝!我在尋找傳輸文件的正確方法時遇到了很多麻煩。這是獲取文件的方式。 (我使用一個使用子句來模擬一個管理員來獲得我想要的訪問權限)。 – user1666231 2013-03-07 22:36:06