123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981 |
-
- Namespace.register("U.UF.UP");
- U.UF.UP.upload = function (inputarr, url, cb, params) {
-
-
-
- }
- U.UF.UP.inputUpload = function (inputarr, url, cb, params) {
- U.UF.UP.uploading(inputarr, cb, params, url);
- }
- U.UF.UP.uploading = function (inputarr, cb, params, url) {
- var _iframe,
- _body = document.body,
- _name = Guid.guidNoDash()
- ;
-
- _iframe = $$("iframe", { "id": _name, "name": _name, "width": 0, "height": 0, "style": { "display": "none" }, "frameBorder": 0 }, _body);
-
- U.UF.C.iframeSameDomain(_iframe, "", function () {
-
- U.UF.UP.inputUpload.submit(_iframe, inputarr, cb, params, url, _name);
- });
- }
- U.UF.UP.inputUpload.submit = function (iframe, inputarr, cb, params, url, name) {
-
- var i, _form, _cloneinput, _changefun, _isfile;
-
- iframe.contentWindow.name = name;
-
- _form = $$("form", { "action": url, "target": name, "encoding": "multipart/form-data", "enctype": "multipart/form-data", "method": "post", "name": "loading", "style": { "display": "none"} }, document.body);
-
- for (i = 0; i < inputarr.length; i++) {
-
- inputarr[i].name = inputarr[i].name || inputarr[i].id || Guid.newGuid();
-
- _cloneinput = inputarr[i].cloneNode();
-
-
- try {
-
- if (document.selection) {
- _cloneinput.select();
- document.selection.clear();
- }
- else {
- _cloneinput.value = "";
- }
- }
- catch (e) { }
-
- if (inputarr[i].parentNode) {
- inputarr[i].parentNode.replaceChild(_cloneinput, inputarr[i]);
- }
-
- _form.appendChild(inputarr[i]);
-
- if (inputarr[i].type == "file") {
- _isfile = true;
- }
- }
-
- if (_isfile !== true) {
- _form.encoding = _form.enctype = "application/x-www-form-urlencoded";
- }
-
- U.UF.DL.iframeLoad(iframe, function () {
- U.selectEl(_form).remove();
- U.UF.UP.inputUpload.asyn(iframe, inputarr, cb, params);
- });
-
- _form.submit();
-
-
-
-
- }
- U.UF.UP.inputUpload.asyn = function (iframe, inputarr, cb, params) {
-
- if (!(U.UF.CI.getBrowser().browser == "msie")) {
- iframe.src = "about:blank";
-
- setTimeout(function () {
- U.UF.UP.inputUpload.callback(iframe, inputarr, cb, params);
- }, 0);
- }
-
- else {
- U.UF.UP.inputUpload.callback(iframe, inputarr, cb, params);
- }
- }
- U.UF.UP.inputUpload.callback = function (iframe, inputarr, cb, params) {
- var _value = U.UF.UP.inputUpload.getData(iframe);
-
- U.selectEl(iframe).remove();
-
- if (U.UF.C.isFunction(cb)) {
-
- cb({
- "value": _value,
- "context": params,
- "inputs": inputarr
- });
- }
- }
- U.UF.UP.inputUpload.getData = function (iframe) {
- var _value = null;
-
- try {
- _value = iframe.contentWindow.document.body.innerHTML;
- }
- catch (e) { }
-
- if (!_value) {
- try {
- _value = iframe.contentWindow.name != iframe.id ? iframe.contentWindow.name : "";
- }
- catch (e) {
- }
- }
-
- if (_value == null) {
- U.alert("服务器处理繁忙,上传失败");
- }
- else {
-
- _value = U.UF.C.toJson(_value);
- }
- return _value;
- }
- U.UF.UP.html5Upload = function (fileinfo, succallback, callback) {
-
- U.UF.UP.html5Upload.fileMd5(fileinfo, function (progress, finish, filehash) {
- if (finish) {
-
- if (U.UF.C.isFunction(callback.getHashCallBack)) {
- callback.getHashCallBack(filehash, fileinfo, succallback, callback.error);
- }
- }
- });
- }
- U.UF.UP.html5Upload.fileMd5 = function (file, callback) {
- var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
- chunkSize = 2097152,
- chunks = Math.ceil(file.size / chunkSize),
- currentChunk = 0,
- spark = new U.UF.EC.SparkMD5.ArrayBuffer(),
- fileReader = new FileReader()
- ;
- file.fileReader = fileReader;
-
- fileReader.onload = function (e) {
- spark.append(e.target.result);
- currentChunk++;
-
- if (currentChunk < chunks) {
- loadNext();
- callback({ 'Loaded': currentChunk, 'Total': chunks })
- }
- else {
- callback({ 'Loaded': 100, 'Total': 100 }, true, spark.end())
- }
- };
-
- fileReader.onerror = function () {
- callback(null, 'oops, something went wrong.')
- };
-
- var loadNext = function () {
- var start = currentChunk * chunkSize,
- end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
- }
- loadNext();
- }
- U.UF.UP.html5Upload.select = function (file, filehash, callback) {
- U.A.Request("http://upload.1473.cn/select", [file.name, filehash, file.size], function (r) {
- callback(r.value.fileServerName, r.value.filesize);
- });
- }
- U.UF.UP.html5Upload.loop = function (file, filehash, fileservername, filesize, callback) {
- var _xhr, _fd, _thunkto, _islast,
- _totalsize = file.size,
- _eachsize = 1024 * 500,
- _thunkfrom = filesize
- ;
-
- if (_eachsize + filesize >= _totalsize) {
- _thunkto = _totalsize;
- _islast = true;
- }
-
- else {
- _thunkto = _eachsize + filesize;
- }
-
- _fd = new FormData();
- _fd.append('theFile', file.slice(_thunkfrom, _thunkto));
- _fd.append('filehash', filehash);
- _fd.append('fileChunk', 0);
- _fd.append('isLastChunk', _islast);
- _fd.append('fileServerName', fileservername);
-
- _xhr = new XMLHttpRequest();
- file.xhr = _xhr;
- _xhr.open("post", "http://upload.1473.cn/upload", true);
- _xhr.onreadystatechange = function () {
- if (_xhr.readyState == 4 && _xhr.status == 200) {
- var _rs = JSON.parse(_xhr.responseText);
- if (_rs.status == 200) {
- callback({ 'Loaded': _thunkto, 'Total': _totalsize });
-
- if (!_rs.isLastChunk) {
-
- U.UF.UP.html5Upload.loop(file, filehash, fileservername, _thunkto, callback)
- }
- else {
- callback(true);
- }
- }
- }
- };
- _xhr.send(_fd);
- }
- U.UF.UP.html5Upload.deleteUploadFile = function (file) {
- if (file.fileReader) {
- file.fileReader.abort();
- }
- if (file.xhr) {
- file.xhr.abort();
- }
- }
- U.UF.UP.html5Upload.deleteUploadAllFile = function () {
- }
- Namespace.register("U.UF.UP.HTML5");
- U.UF.UP.HTML5.upload = function (url, file, callback, md5callback, upprogresscallback) {
-
- U.UF.UP.HTML5.fileMd5(file, function (progress, filehash) {
-
- if (progress.Loaded == "error") {
- console.log("上传错误:" + progress.Total);
- }
-
- if (progress.Loaded == "success") {
-
-
- file.url = url;
- file.fileHash = filehash;
-
- U.A.Request("http://upload.1473.cn/select", [file.name, file.fileHash, file.size], function (r) {
- if (r.value) {
-
- var _result = r.value;
-
- file.fileServerName = _result.fileServerName;
-
- file.uploadedSize = _result.filesize;
-
- U.UF.UP.HTML5.loop(file.url, file, file.fileHash, file.fileServerName, file.uploadedSize,
-
- function (progress) {
-
- if (progress.Loaded == "success") {
-
- callback(file);
- }
-
- else {
-
- upprogresscallback(progress);
- }
- }
- );
- }
- else {
- U.alert("上传异常,服务器未响应,此处需要做处理!");
- }
- });
- }
-
- else {
-
- md5callback(progress);
- }
- });
- }
- U.UF.UP.HTML5.upload.Api = function (url, file, fileservername, uploadedsize, callback, md5callback, upprogresscallback) {
-
- U.UF.UP.HTML5.fileMd5(file, function (progress, filehash) {
-
- if (progress.Loaded == "error") {
- console.log("上传错误:" + progress.Total);
- }
-
- if (progress.Loaded == "success") {
-
-
- file.url = url;
- file.fileHash = filehash;
-
- file.fileServerName = fileservername;
-
- file.uploadedSize = uploadedsize;
-
- U.UF.UP.HTML5.loop(file.url, file, file.fileHash, file.fileServerName, file.uploadedSize,
-
- function (progress) {
-
- if (progress.Loaded == "success") {
-
- callback(file);
- }
-
- else {
-
- upprogresscallback(progress);
- }
- }
- );
- }
-
- else {
-
- md5callback(progress);
- }
- });
- }
- U.UF.UP.HTML5.fileMd5 = function (file, callback) {
- var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
- chunkSize = 2097152,
- chunks = Math.ceil(file.size / chunkSize),
- currentChunk = 0,
- spark = new U.UF.EC.SparkMD5.ArrayBuffer(),
- fileReader = new FileReader()
- ;
- file.fileReader = fileReader;
-
- fileReader.onload = function (e) {
- spark.append(e.target.result);
- currentChunk++;
-
- if (currentChunk < chunks) {
-
- loadNext();
-
- callback({ 'Loaded': currentChunk, 'Total': chunks })
- }
- else {
-
- callback({ 'Loaded': "success", 'Total': 100 }, spark.end())
- }
- };
-
- fileReader.onerror = function () {
- callback({ 'Loaded': "error", 'Total': 'oops, something went wrong.' })
- };
-
- var loadNext = function () {
- var start = currentChunk * chunkSize,
- end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
- }
- loadNext();
- }
- U.UF.UP.HTML5.selectFromDB = function (file, filehash, callback) {
-
-
- U.A.Request(US.DISK, ["GetFileByHash", filehash, file.size, file.name.getExtendName()], function (r) {
-
- if (r.value) {
- }
- else {
-
-
- U.A.Request("http://upload.1473.cn/select", [file.name, filehash, file.size], function (r) {
-
- if(r.value){
- }
- else {
-
- callback(r.value.fileServerName, r.value.filesize);
- }
- });
- }
- });
- }
- U.UF.UP.HTML5.loop = function (url,file, filehash, fileservername, filesize, callback) {
- var _xhr,
- _fd,
- _thunkto,
- _islast=false,
- _totalsize = file.size,
- _eachsize = 1024 * 500,
- _count = filesize / _eachsize,
- _thunkfrom = filesize
- ;
-
- if (filesize >= _totalsize) {
- _thunkto = _totalsize;
- _islast = true;
- }
-
- else {
- _thunkto = _eachsize + filesize;
- }
-
- _fd = new FormData();
- _fd.append('theFile', file.slice(_thunkfrom, _thunkto));
- _fd.append('filehash', filehash);
- _fd.append('fileChunk', _count);
- _fd.append('isFirstChunk', _count);
- _fd.append('isLastChunk', _islast);
- _fd.append('fileServerName', fileservername);
-
- _xhr = new XMLHttpRequest();
- file.xhr = _xhr;
- _xhr.open("post", url, true);
-
- _xhr.onreadystatechange = function () {
- if (_xhr.readyState == 4 && _xhr.status == 200) {
- var _rs = JSON.parse(_xhr.responseText);
- if (_rs.status == 200) {
- callback({ 'Loaded': _thunkto, 'Total': _totalsize });
-
- if (_rs.isLastChunk) {
-
- callback({ 'Loaded': "success", 'Total': _totalsize });
- return;
- }
-
- U.UF.UP.HTML5.loop(url, file, filehash, fileservername, _thunkto, callback)
- }
- else {
- U.alert("上传服务器出错!");
- }
- }
- };
- _xhr.send(_fd);
- }
- U.UF.UP.HTML5.stopUploadFile = function (file) {
- if (file.fileReader) {
- file.fileReader.abort();
- }
- if (file.xhr) {
- file.xhr.abort();
- }
- }
- U.UF.UP.HTML5.deleteUploadAllFile = function () {
- }
- U.UF.UP.flashbottom = {};
- U.UF.UP.addFlashUploadBottom = function (el, name, style, callback, uploadtype) {
- var _divel,
- _width = style._width,
- _height = style._height,
- _uploadbootom = U.selectEl("object", el)[0];
- _name = name || Guid.newGuid();
-
- if (!_uploadbootom) {
- _divel = $$("div", {
- "innerHTML": "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' class='U_MD_D_filePrew' _width='" + style._width + "' _height='" + style._height + "' id='" + _name + "' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0'>" +
- "<param name='movie' value='/img/upload.swf' />" +
- "<param name='menu' value='false' />" +
- "<param name='wmode' value='Opaque' />" +
- "<param name='quality' value='high' />" +
- "<param name='bgcolor' value='ffffff' />" +
- "<param name='allowScriptAccess' value='always' />" +
- "<embed src='/img/upload.swf' quality='high' bgcolor='ffffff' _width='" + style._width + "' _height='" + style._height + "' name='" + _name + "' align='middle' play='true' wmode='transparent' loop='false' quality='high' style='z-index: 0;' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer'>" +
- "</embed>" +
- "</object>"
- });
- _uploadbootom = _divel.firstChild;
- el.insertBefore(_uploadbootom, el.firstChild);
- }
-
- U.UF.UP.flashbottom[name] = {
- uploadtype: uploadtype || "*",
- flashbottom: U.selectEl("*" + name, el)[0],
- style: style || {},
- callback: callback || {}
- };
- return _uploadbootom;
- }
- U.UF.UP.flashLoad = function () {
- var i,
- _obj = U.UF.UP.flashbottom;
-
- for (i in _obj) {
-
- if (_obj[i].flashbottom.setUploadStyle) {
- _obj[i].flashbottom.setUploadStyle(i, _obj[i].style, {}, _obj[i].uploadtype);
- }
- }
- }
- U.UF.UP.error = function (e) {
- U.alert(e);
- }
- U.UF.UP.select = function (bottomid, filesarr) {
- var _obj = U.UF.UP.flashbottom[bottomid];
- _obj.callback.select(filesarr);
- }
- U.UF.UP.generateFileHash = function (flashbottom, fileid, i, j) {
- flashbottom.generateFileHash(fileid, 0, 0);
- }
- U.UF.UP.getHashCallBack = function (md5value, uploadid, fileid) {
- var _obj = U.UF.UP.flashbottom[uploadid];
- _obj.callback.getHashCallBack(md5value, fileid);
- }
- U.UF.UP.progress = function (speedobj, uploadid, fileid, str) {
- var _obj = U.UF.UP.flashbottom[uploadid];
- _obj.callback.progress(speedobj, fileid, str);
- }
- U.UF.UP.flashUpload = function (flashbottom, fileid, url, i, j) {
- flashbottom.upload(url, fileid, i, j);
- }
- U.UF.UP.uploadComplete = function (e, uploadid, fileid) {
- var _obj = U.UF.UP.flashbottom[uploadid];
- _obj.callback.uploadComplete(e, fileid);
- }
- U.UF.UP.getData = function (request, uploadid, fileid) {
- var _obj = U.UF.UP.flashbottom[uploadid];
- _obj.callback.getData(request, fileid);
- }
- U.UF.UP.endUpload = function (uploadid) {
- var _obj = U.UF.UP.flashbottom[uploadid];
- _obj.callback.endUpload();
- }
- U.UF.UP.deleteUploadFile = function (flashbottom, i, j) {
- flashbottom.deleteUploadFile(i, j);
- }
- U.UF.UP.deleteReferenceFile = function (flashbottom, i) {
- flashbottom.deleteReferenceFile(i);
- }
- U.UF.UP.deleteUploadAllFile = function (flashbottom) {
- flashbottom.deleteUploadAllFile();
- }
- U.UF.UP.download = function (filename, filepath, url) {
- var _filenameinput = $$("input", { "type": "text", "value": encodeURIComponent(filename), "name": "filename" }),
- _filepathinput = $$("input", { "type": "text", "value": filepath, "name": "filepath" });
-
- U.UF.UP.inputUpload([_filenameinput, _filepathinput], url);
- }
- U.UF.UP.getFileNameAndExtension = function (filename) {
- filename = filename.toLocaleLowerCase();
- var _num = filename.lastIndexOf("."),
- _index = filename.lastIndexOf("\\"),
- _xindex = filename.lastIndexOf("/"),
- _name = filename.substr((_index > _xindex ? _index : _xindex) + 1, _num),
- _extension = filename.substr(_num + 1)
- ;
- return [_name, _extension];
- }
- U.UF.UP.isImg = function (name) {
- var _extension = U.UF.UP.getFileNameAndExtension(name.toLowerCase())[1];
- return ["jpg", "gif", "png", "bmp", "jpeg"].indexOf(_extension) > -1;
- }
- U.UF.UP.minUnitToMaxUnit = function (filesize) {
- var _unitarr = ["B", "KB", "M", "G", "TB"],
- _unit = (filesize + "").replace(U.UF.S.Num, ""),
- i = _unitarr.indexOf(_unit) < 0 ? 0 : _unitarr.indexOf(_unit);
- filesize = parseFloat(filesize);
-
- while (filesize > 1024) {
- filesize /= 1024;
- i++;
- }
- return parseInt(filesize.toFixed(2)) + _unitarr[i];
- }
- U.UF.UP.maxUnitToByte = function (filesize) {
- var _unitarr = ["B", "KB", "M", "G", "TB"],
- _unit = (filesize + "").replace(U.UF.S.Num, ""),
- i = _unitarr.indexOf(_unit);
- i = i < 0 ? 0 : i;
- return parseFloat(filesize) * Math.pow(1024, i);
- }
|