在業務場景中檔案上傳很普遍,而大檔案的上傳經常會導致上傳時長過久,大量占用頻寬資源,而分片上傳就解決了目前的問題。
import react from 'react';
import from './utils/upload';
import concurrentutil from './utils/concurrent'
import "./file.css";
export default class filecontainer extends react.component ;
}onchange = (event) => );
this.concurrent.setstate();
event.preventdefault();
}handlesubmit = async (event) => = this.state.file;
console.log("start at: -----> ", starttime);
//1.請求服務端,查詢對應檔案塊
const } = await uploadfetch(
})this.concurrent.handleupload(pathlist).then((res) => );
}/**
* @description 檔案上傳
*/uploadrequest=()=>})}
/**
* @description 合併請求
*/mergerequest=()=>,})}
render() )}
}
import promiselimit from './promiselimit'
/** * @description 檔案分片上傳util
* @export
* @class concurrent
*/export default class concurrent upload
* @param merge
* @param [size=1024 * 1024 * 1]
* @param [limit=4]
* @memberof concurrent
*/constructor(
upload,
merge,
size = 1024 * 1024 * 1, //預設 1m
limit = 4, //併發限制 預設 4
) ;}
/*** @param obj
* @memberof concurrent
*/setstate(obj) ;
}/**
* @description 檔案分片
* @returns
* @memberof concurrent
*/splitzip() = this.state;
const filesplit = ;
if (file.size > size)
} else
this.setstate();
return filesplit;
}/**
* @description 上傳行為;如果 pathlist 為 falsy或 則上傳當前所有分片
* @memberof concurrent
*/handleupload = async (pathlist) => = this.state,
= file,
filemark = "";
//檔案過濾
filesplit = filesplit.map((blob, index) => ` //檔案分片標識
if ((array.isarray(pathlist) && !pathlist.find(pp => pp.split("_index_")[1] === `$`)) || !pathlist) _index_$`, file_name }
} else
})filesplit = filesplit.filter(item => item);
// 沒有缺失的片段 ,傳送合併請求
if (array.isarray(filesplit) && filesplit.length === 0) )
}//2.上傳缺失的檔案塊,promise 限流
const promiselimit = new promiselimit(this.limit, filesplit, this.state.upload)
await promiselimit.excute();
return this.state.merge.call(this, )
}/**
* @description 分片上傳
* @param filesequenceitem
* @param file_name
* @memberof concurrent
*/sequenceupload = (filesequenceitem, file_name) => )))
}}
/**
* @description promise限流
* * @export
* @class promiselimit
*/export default class promiselimit
sequence = async () =>
const paramitem = this.params[this.i++];
const p = promise.resolve().then(() => this.iteratorfunc(paramitem));
this.sequenceret.push(p);
// 如果執行完畢,從執行佇列中刪除
const e = p.then(() => this.sequenceexcuting.splice(this.sequenceexcuting.indexof(e), 1));
this.sequenceexcuting.push(e);
let r = promise.resolve();
// 執行佇列》= 限制併發數時進行觸發,結束後遞迴假如新的promise例項
if (this.sequenceexcuting.length >= this.limit)
await r;
return this.sequence();
}excute = async () =>
}
完整**詳見githubgithub.com
實現效果:
js 即時上傳php js php分片上傳大檔案
這篇文章介紹的內容是關於js php分片上傳大檔案,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下 1.理解部分 服務端為什麼不能直接傳大檔案?跟php.ini裡面的幾個配置有關upload max filesize 2m php最大能接受的檔案大小 post max size 8m ...
httpclient 上傳檔案
3.0版本 用multipartrequestentity方式,怎麼搞都不行。最後還是用了multipartpostmethod,才算搞定 總之,不好使啊.具體使用方式如下 2,post.addrequestheader content type multipart form data charse...
HttpClient上傳檔案
httpclient post請求 上傳多 檔案 param url 請求位址 param params 引數列表 return 響應字串 throws unsupportedencodingexception author jie date 2015 2 12 public static stri...