2017-01-31 2 views
0

私は助けてください。 私は次のような方法角度2(クライアント)を使用してファイルをダウンロードするmvc api(サーバー)

[HttpGet] 
    public HttpResponseMessage Download(int id) 
    { 
     var url = @"\\\\srv\\temp\\201589.zip" 
     var fileInfo = new FileInfo(url); 
     using (MemoryStream ms = new MemoryStream()) 
     { 
     using (FileStream file = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read)) 
     { 
      byte[] bytes = new byte[file.Length]; 
      file.Read(bytes, 0, (int)file.Length); 
      ms.Write(bytes, 0, (int)file.Length); 

      HttpResponseMessage httpResponseMessage = new HttpResponseMessage(); 
      httpResponseMessage.Content = new ByteArrayContent(bytes.ToArray()); 
      httpResponseMessage.Content.Headers.Add("x-filename", fileInfo.FullName); 
      httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 
      httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
      httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileInfo.FullName; 
      httpResponseMessage.StatusCode = HttpStatusCode.OK; 

      return httpResponseMessage; 
      } 
      return null; 
     } 
    } 

私は、同じウィンドウでファイルをsownloadingするために、クライアント側で何をすべきを持って角度2.サーバー側で

を使用してファイルをダウンロードする必要がユーザーは

let params = new URLSearchParams(); 

    params.append("id", 1); 

    this.http.get("serverUrl/Download", {search: params}).subscribe((data: any) => {}); 

(ない新しいウィンドウまたはタブで)ブラウザの下のバーでそれを見ることができます最終的に私が作成した

答えて

0

ありがとう隠しiframeを持つ共有コンポーネントがsrcを取得してファイルをダウンロードするようにします...

関連する問題