2017-04-19 25 views
0

ここAsp.net APIからのファイルのダウンロード中のエラーは私 が指定されたパスの形式はサポートされていません。

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のはファイルに対してのみ動作します私を助けてくださいWEBAPI からではなく、The given path's format is not supported.としてダウンロードイム取得エラー時にダウンロードExcelシートのためのシンプルなC#のコードを書きました仮想ディレクトリ内で、プロジェクト外のファイルではありません。 プロジェクトに必要なファイルを常に含めてからmapathを使います。
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

内でそのprsenet型「System.NotSupportedException」の例外は、これはEXAMPLE.SQLのファイルを検索します//がmscorlib.dllで発生したが、ユーザーコード – Hussain

+0

で処理されていませんでしたルートディレクトリに保存します。 // ...これは、アプリケーションの任意のディレクトリにあるファイルで使用できます。 'string localFilePath = HttpContext.Current.Request.MapPath("〜/ Example.txt ");'。考えましたか?だからあなたのケースでは '3c575edb-bf53-470c-a8b6-aa4234e796c0'フォルダですか? –

関連する問題