2017-04-19 448 views
0

這裏下載文件時出錯我寫了簡單的C#代碼下載Excel工作表從的WebAPI 但下載即時得到誤差The given path's format is not supported.期間 請幫我不支持給定路徑的格式。從Asp.net阿比

public HttpResponseMessage GetTestFile() 
     { 
      HttpResponseMessage result = null; 
     // var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0"); 
      var localFilePath = HttpContext.Current.Server.MapPath(@"~/C:/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0"); 


      if (!File.Exists(localFilePath)) 
      { 
       result = Request.CreateResponse(HttpStatusCode.Gone); 
      } 
      else 
      { 
       // Serve the file to the client 
       result = Request.CreateResponse(HttpStatusCode.OK); 
       result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read)); 
       result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); 
       result.Content.Headers.ContentDisposition.FileName = "SampleImg"; 
      } 

      return result; 
     } 

回答

1

MapPath方法僅適用於文件在您的虛擬目錄中,而不是項目之外的文件。 因此,請始終將您需要的文件包含到項目中,然後使用mappath。
var localFilePath = HttpContext.Current.Server.MapPath("~/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0");

localFilePath //it will contain your_project_path/Temp/3c575edb-bf53-470c-a8b6-aa4234e796c0 
e.g: F:\projectname\Temp\3c575edb-bf53-470c-a8b6-aa4234e796c0 
+0

是它我的項目 – Hussain

+0

「System.NotSupportedException」類型的異常之內prsenet出現在mscorlib.dll,但在用戶代碼中沒有處理 – Hussain

+0

//這將查找文件example.txt的在根目錄下。 // ...這可以用在應用程序中任何目錄中的文件中。 'string localFilePath = HttpContext.Current.Request.MapPath(「〜/ Example.txt」);'。有想法?所以你的情況是'3c575edb-bf53-470c-a8b6-aa4234e796c0'文件夾? –

相關問題