最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Download a PNG file served as binaryoctet-stream - Stack Overflow

programmeradmin3浏览0评论

steps that will reproduce the problem :

I am saving a Blob object as form data using a service and I am receiving the response as content-type: application/octet-stream as in attached image

What is the expected result?

  • To download and view the the application/octet-stream as a image into local machine and view it using applications

What happens instead?

able to download the file as image but it says we dont support this file format though its (ex:image.png)

function add() {
  $.ajax({
  url: 'https://localhost:3000/upload/sampleImage.png',
  type: 'GET',
  success: function (data) {
    const link = document.createElement('a');
    link.style.display = 'none';
    link.download = "sample image";
     link.href =  
       'data:' +
       'image/png' +
       ';base64,' +
       window.btoa(unescape(encodeURIComponent(data)));
    link.click();
  },
  error: function (request, error) {
    alert("Request: " + JSON.stringify(request));
  }
});

}

Any ways to download the file and preview it successfully

steps that will reproduce the problem :

I am saving a Blob object as form data using a service and I am receiving the response as content-type: application/octet-stream as in attached image

What is the expected result?

  • To download and view the the application/octet-stream as a image into local machine and view it using applications

What happens instead?

able to download the file as image but it says we dont support this file format though its (ex:image.png)

function add() {
  $.ajax({
  url: 'https://localhost:3000/upload/sampleImage.png',
  type: 'GET',
  success: function (data) {
    const link = document.createElement('a');
    link.style.display = 'none';
    link.download = "sample image";
     link.href =  
       'data:' +
       'image/png' +
       ';base64,' +
       window.btoa(unescape(encodeURIComponent(data)));
    link.click();
  },
  error: function (request, error) {
    alert("Request: " + JSON.stringify(request));
  }
});

}

Any ways to download the file and preview it successfully

Share edited Mar 29, 2020 at 14:16 Kishore Jv asked Mar 29, 2020 at 14:02 Kishore JvKishore Jv 1596 silver badges17 bronze badges 2
  • 2 you mean you want to http get the file in your folder and download it ? and why are you using $ ajax in angular ....we can use httpclient right?? – M A Salman Commented Mar 29, 2020 at 14:07
  • I wanted to directly download the file after receiving the response. for test purpose i used $ajax, yes can use httpclient as well – Kishore Jv Commented Mar 29, 2020 at 14:15
Add a ment  | 

1 Answer 1

Reset to default 4

Set the responseType as blob in request

Using HttpClient:

this.http.get("https://localhost:3000/upload/sampleImage.png",{responseType:'blob'}).subscribe((img)=>{
        const link = document.createElement('a');
        link.style.display = 'none';
        link.download = "sample image";
         link.href =window.URL.createObjectURL(data);
        link.click();
  });

Now using $.ajax(not remended,avoid using it) specify the dataType as blob and use window.URL.createObjectURL(data) to create URL

 $.ajax({
  url: 'https://localhost:3000/upload/sampleImage.png',
  type: 'GET',
  xhrFields:{
            responseType: 'blob'
        },
  success: function (data) {
    const link = document.createElement('a');
    link.style.display = 'none';
    link.download = "sample image";
    var blob = new Blob([data], {type: 'image/png'});
    link.href =window.URL.createObjectURL(blob);
    link.click();
  },
  error: function (request, error) {
    alert("Request: " + JSON.stringify(request));
  }
发布评论

评论列表(0)

  1. 暂无评论