Handle file download from ajax post

前端 未结 20 2599
心在旅途
心在旅途 2020-11-21 05:46

I have a javascript app that sends ajax POST requests to a certain URL. Response might be a JSON string or it might be a file (as an attachment). I can easily detect Content

20条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:16

    If response is an Array Buffer, try this under onsuccess event in Ajax:

     if (event.data instanceof ArrayBuffer) {
              var binary = '';
              var bytes = new Uint8Array(event.data);
              for (var i = 0; i < bytes.byteLength; i++) {
                  binary += String.fromCharCode(bytes[i])
              }
              $("#some_id").append("
  • "); return; }
    • where event.data is response received in success function of xhr event.

提交回复
热议问题