Prompt file download with XMLHttpRequest

后端 未结 3 1927
清歌不尽
清歌不尽 2020-12-08 00:36

I\'m aware that jQuery\'s ajax method cannot handle downloads, and I do not want to add a jQuery plugin to do this.

I want to know how to send POST data with XMLHttp

3条回答
  •  清歌不尽
    2020-12-08 01:20

    download: function(){
        var postData = new FormData();
    		var xhr = new XMLHttpRequest();
    		xhr.open('GET', downloadUrl, true);
    		xhr.responseType = 'blob';
    		xhr.onload = function (e) {
    			var blob = xhr.response;
    			this.saveOrOpenBlob(blob);
    		}.bind(this)
    		xhr.send(postData);
     }
    
    saveOrOpenBlob: function(blob) {
    		var assetRecord = this.getAssetRecord();
    		var fileName = 'Test.mp4'
    		var tempEl = document.createElement("a");
        	document.body.appendChild(tempEl);
        	tempEl.style = "display: none";
            url = window.URL.createObjectURL(blob);
            tempEl.href = url;
            tempEl.download = fileName;
            tempEl.click();
    		window.URL.revokeObjectURL(url);
    	},

    Try this it is working for me.

提交回复
热议问题