2016-05-23 22 views
0

多分本当に単純な解決策があります...しかし、私が見たスレッドの多くは私の問題を解決しません...私はしようとします可能な限り明確にすること。 Asp.Net WEBAPIプロジェクトで(jquery)からファイルを取得する方法Asp.NetのWebApi、Asp.Net IDで承認されました

現在のように見ている「DownloadController」があります:単純なHTTP GET(ブラウザでちょうどURL)が行われたときにコードのこの作品は完全に働いた

private HttpResponseMessage CreateFileResponse(string fileName, string originalName, List<string> allowedMimes) 
    { 
     // see if file exists 
     if (!_FileProvider.Exists(fileName)) 
     { 
      return Request.CreateResponse(HttpStatusCode.NotFound); 
     } 

     // retrieve mimeType 
     string mimeType = MimeMapping.GetMimeMapping(fileName); 
     if (!allowedMimes.Contains(mimeType)) 
     { 
      mimeType = _generalMimeType; 
     } 

     // open the file 
     FileStream fileStream = _FileProvider.Open(fileName); 

     // create the response 
     var response = new HttpResponseMessage(); 
     response.Content = new StreamContent(fileStream); 
     response.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); 
     response.Content.Headers.ContentLength = _FileProvider.GetLength(fileName); 

     // if the mimetype is not allowed, download as attachment 
     if (mimeType == _generalMimeType) 
     { 
      response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
      response.Content.Headers.ContentDisposition.FileName = originalName; 
     } 

     return response; 
    } 

    //for understanding the above 
    public DownloadController() 
    { 
     _FileProvider = new FileProvider(); 
    } 

    public interface IFileProvider 
    { 
     bool Exists(string name); 
     FileStream Open(string name); 
     long GetLength(string name); 
    } 

は、コントローラ上の特定のルートに移動します。

[Route("Download/Document/{documentid}")] 
    public HttpResponseMessage GetDocument(Guid documentid) 
    { 
     - 
     - 
     - 
     - 
    return this.CreateFileResponse(fileName, document.filename, allowedMimes);  
    } 

しかし、すべてのコントローラはすべてasp.net IDの[Authorize]フィールドで承認されました。すべてのリクエストには、そこにトークン(ベアラ "トークン")を持つAuthorizationフィールドが必要です。ほとんどすべてのリクエストはヘッダフィールドを追加するだけでjqueryで完了します。しかし、ファイルをダウンロードするために、これは行われませんでした。 DownloadControllerのトークンヘッダーでjqueryを使用すると、応答は得られます。

PNG↵IHDR sRGB gAMA apHYs o.......

また、フィンドラーと郵便配達員は、トークンを要求すると実際のイメージを表示することができます。 (pdfファイルやその他のファイルについてはわかりません)

しかし、このデータを私がstackoverflowで試した特定のJavaScriptソリューションで設定するとうまくいきません....画像が壊れています..誰かが解決策を教えてくれますか?このデータをjavascriptから設定しますか?:)。もちろん、他の解決策もあります。フィドラーから

インターセプト応答: Intercepted response from Fiddler

+0

あなたは何とかあなたのHTMLコードのAPIからの応答を表示しようとしていますか?応答はバイトのストリームですか? – Pushpendra

+0

@Pushpendraはい、多少長い話です。しかし、ちょうど明確になりたかった... – Riddim

答えて

0

htmlresponseにストリームを変換し、そのようなgroupdocsなどのツール、画像、あなたのhtmlコードにレンダリングするために使用できるネイティブがあります。おそらく、バイトストリームとしてレスポンスを送信するAPIコールでこれを行うことができます。 groupdocs-library

+0

かなりいいね。しかし、私はそのような有料のソリューションに行くことなく可能でなければならないと考えています。 – Riddim

+0

私が知っている1つの方法は、base64エンコーディングに使用されています。このリンクは、ストリームをimgのデータ属性またはsrc属性に入れてみるのに役立ちます。 http://stackoverflow.com/questions/600807/does-html-allow-for-embedded-encoding-of-images-by-stream – Pushpendra

関連する問題