2013-03-18 78 views
0

我有解決方案「解決方案」和兩個項目:訪問文件

  • solution.WebUI(這裏用戶上傳文件到像「〜/上傳」某個文件夾
  • solution.WebApi(這裏我必須訪問用戶文件)

在網頁API項目中,我訪問文件就像這樣:

public HttpResponseMessage GetPdfPage() 
    { 
     HttpResponseMessage responce = new HttpResponseMessage(); 
     responce.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath("~/somefile.pdf"), FileMode.Open, FileAccess.Read)); 
     responce.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); 

     return responce; 
    } 

如何修改文件路徑?

+0

我不明白。 – 2013-03-18 16:45:16

回答

0

我也遇到了類似的問題,我需要從下相同的解決方案的BLL項目訪問另一個項目(主)的上傳文件夾中。 爲此我已經使用絕對路徑(非硬編碼)的上傳文件夾。我認爲下面的代碼也適用於你。

public HttpResponseMessage GetPdfPage() 
    { 
     HttpResponseMessage responce = new HttpResponseMessage(); 
     string basePath = System.AppDomain.CurrentDomain.BaseDirectory; 
     responce.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath(basePath +"somefile.pdf"), FileMode.Open, FileAccess.Read)); 
     responce.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); 

     return responce; 
    }