Javascript 如何在不保存到磁盘的情况下下载和上载zip文件
我正在尝试下载并立即上传一个zip文件。然而,上载端点不会将接收到的文件识别为zip,尽管它是作为zip下载的,但被视为类型字符串。我需要一种方法来处理文件,就像我承诺的那样,无需解压缩。有什么想法吗?您可以像这样以二进制格式获取数据Javascript 如何在不保存到磁盘的情况下下载和上载zip文件,javascript,jquery,reactjs,Javascript,Jquery,Reactjs,我正在尝试下载并立即上传一个zip文件。然而,上载端点不会将接收到的文件识别为zip,尽管它是作为zip下载的,但被视为类型字符串。我需要一种方法来处理文件,就像我承诺的那样,无需解压缩。有什么想法吗?您可以像这样以二进制格式获取数据 $.ajax({ url: 'url.com/myfile.zip', }) .then((data) => { const blob = new Blob([parsed_data], {type: 'ap
$.ajax({
url: 'url.com/myfile.zip',
})
.then((data) => {
const blob = new Blob([parsed_data], {type: 'application/octet-stream'});
const file = new File([blob], filename, {type: 'application/zip'});
this.handleUpload(file); // Sends POST request with received file
});
您可以像这样以二进制格式获取数据
$.ajax({
url: 'url.com/myfile.zip',
})
.then((data) => {
const blob = new Blob([parsed_data], {type: 'application/octet-stream'});
const file = new File([blob], filename, {type: 'application/zip'});
this.handleUpload(file); // Sends POST request with received file
});
您可以将响应用作
blob
,如中所述
然后(通常)使用FormData进行上传
/**
* Downloading the file
*
* @param {string} file - The url of the target file
* @param callback - Callback for when we are ready
*/
function download( file, callback ) {
const request = new XMLHttpRequest();
request.open( "GET", file, true );
request.responseType = "arraybuffer";
request.onreadystatechange = () => {
/** Do nothing if we are not ready yet */
if ( request.readyState !== XMLHttpRequest.DONE ) { return; }
if ( request.status === 200 ) {
callback( new Blob( [request.response], { type : "application/zip" } ) );
} else {
console.error( request.status );
callback( false );
}
};
request.send();
}
总而言之:
/**
* Uploading the file
* @param {Blob} blob. - A blob of file
* @param {string} filename - The file name
* @param {string} url. - The upload rest point
* @param callback - Callback for when we are ready
*/
function upload( blob, filename, url, callback ) {
const formData = new FormData(),
request = new XMLHttpRequest();
/** Configure the request */
request.open( "POST", url, true );
formData.append( "file", blob, filename );
/** Sets the callback */
request.onreadystatechange = () => {
/** Do nothing if we are not ready yet */
if ( request.readyState !== XMLHttpRequest.DONE ) { return; }
/** Sends back the response */
if ( request.status === 200 ) {
callback( true );
} else {
console.error( request.status );
callback( false );
}
};
/** Send the request */
request.send( formData );
}
您可以将响应用作
blob
,如中所述
然后(通常)使用FormData进行上传
/**
* Downloading the file
*
* @param {string} file - The url of the target file
* @param callback - Callback for when we are ready
*/
function download( file, callback ) {
const request = new XMLHttpRequest();
request.open( "GET", file, true );
request.responseType = "arraybuffer";
request.onreadystatechange = () => {
/** Do nothing if we are not ready yet */
if ( request.readyState !== XMLHttpRequest.DONE ) { return; }
if ( request.status === 200 ) {
callback( new Blob( [request.response], { type : "application/zip" } ) );
} else {
console.error( request.status );
callback( false );
}
};
request.send();
}
总而言之:
/**
* Uploading the file
* @param {Blob} blob. - A blob of file
* @param {string} filename - The file name
* @param {string} url. - The upload rest point
* @param callback - Callback for when we are ready
*/
function upload( blob, filename, url, callback ) {
const formData = new FormData(),
request = new XMLHttpRequest();
/** Configure the request */
request.open( "POST", url, true );
formData.append( "file", blob, filename );
/** Sets the callback */
request.onreadystatechange = () => {
/** Do nothing if we are not ready yet */
if ( request.readyState !== XMLHttpRequest.DONE ) { return; }
/** Sends back the response */
if ( request.status === 200 ) {
callback( true );
} else {
console.error( request.status );
callback( false );
}
};
/** Send the request */
request.send( formData );
}
为什么不直接从给定的url“获取”后端的zip呢?会更简单为什么你想做这个好主意?你想让你的网站有更高的流量吗?@HugoRegibo我有客户端需要它,然后将它发送到后端服务器。“get”可以工作,但我获取的数据是二进制的,似乎不再被识别为zip。@madalinivascu你知道我如何让它为我实现吗?:-)handleUpload函数在哪里?为什么不直接从给定的url“获取”后端的zip文件?会更简单为什么你想做这个好主意?你想让你的网站有更高的流量吗?@HugoRegibo我有客户端需要它,然后将它发送到后端服务器。“get”可以工作,但我获取的数据是二进制的,似乎不再被识别为zip。@madalinivascu你知道我如何让它为我实现吗?:-)handleUpload功能在哪里?现在可以正常工作了。在创建新blob和新文件时,请记住将
数据
和blob
包装在一个数组中。谢谢,现在很好用。在创建新blob和新文件时,请记住将数据
和blob
包装在一个数组中。谢谢