Commit e0e3aafe authored by Sergey.Konovalov's avatar Sergey.Konovalov Committed by Alexander.Trofimov

image upload вместо form submit используем xhr2 send, чтобы можно было...

image upload вместо form submit используем xhr2 send, чтобы можно было корректно обрабатывать ответы от сервера не с 200 статусом и для бага Bug 13525 - Бесконечный прогресс загрузки изображения с локального диска при отсутствии интернета

git-svn-id: svn://192.168.3.15/activex/AVS/Sources/TeamlabOffice/trunk/OfficeWeb@64905 954022d7-b5bf-4e40-9824-e11837661b57
parent b7d69272
...@@ -58,6 +58,7 @@ if (typeof String.prototype.repeat !== 'function') { ...@@ -58,6 +58,7 @@ if (typeof String.prototype.repeat !== 'function') {
var g_oZipChanges = null; var g_oZipChanges = null;
var g_sDownloadServiceLocalUrl = "/downloadas"; var g_sDownloadServiceLocalUrl = "/downloadas";
var g_sUploadServiceLocalUrl = "/upload"; var g_sUploadServiceLocalUrl = "/upload";
var g_sUploadServiceLocalUrlOld = "/uploadold";
var g_nMaxRequestLength = 5242880;//5mb <requestLimits maxAllowedContentLength="30000000" /> default 30mb var g_nMaxRequestLength = 5242880;//5mb <requestLimits maxAllowedContentLength="30000000" /> default 30mb
var g_cCharDelimiter = String.fromCharCode(5); var g_cCharDelimiter = String.fromCharCode(5);
...@@ -616,27 +617,39 @@ function InitOnMessage (callback) { ...@@ -616,27 +617,39 @@ function InitOnMessage (callback) {
}, false); }, false);
} }
} }
function ShowImageFileDialog (documentId, documentUserId, callback) { function ShowImageFileDialog (documentId, documentUserId, callback, callbackOld) {
var frameWindow = GetUploadIFrame(); var fileName;
var content = '<html><head></head><body><form action="'+g_sUploadServiceLocalUrl+'/'+documentId+'/'+documentUserId+'/'+g_oDocumentUrls.getMaxIndex()+'" method="POST" enctype="multipart/form-data"><input id="apiiuFile" name="apiiuFile" type="file" accept="image/*" size="1"><input id="apiiuSubmit" name="apiiuSubmit" type="submit" style="display:none;"></form></body></html>'; if ("undefined" != typeof(FileReader)) {
frameWindow.document.open(); fileName = GetUploadInput(function (e) {
frameWindow.document.write(content); if (e && e.target && e.target.files) {
frameWindow.document.close(); var nError = ValidateUploadImage(e.target.files);
callback(nError, e.target.files);
var fileName = frameWindow.document.getElementById("apiiuFile"); } else {
var fileSubmit = frameWindow.document.getElementById("apiiuSubmit"); callback(c_oAscServerError.Unknown);
fileName.onchange = function (e) {
if (e && e.target && e.target.files) {
var nError = ValidateUploadImage(e.target.files);
if (c_oAscServerError.NoError != nError) {
callback(g_fMapAscServerErrorToAscError(nError));
return;
} }
} });
callback(c_oAscServerError.NoError); } else {
fileSubmit.click(); var frameWindow = GetUploadIFrame();
}; var content = '<html><head></head><body><form action="'+g_sUploadServiceLocalUrlOld+'/'+documentId+'/'+documentUserId+'/'+g_oDocumentUrls.getMaxIndex()+'" method="POST" enctype="multipart/form-data"><input id="apiiuFile" name="apiiuFile" type="file" accept="image/*" size="1"><input id="apiiuSubmit" name="apiiuSubmit" type="submit" style="display:none;"></form></body></html>';
frameWindow.document.open();
frameWindow.document.write(content);
frameWindow.document.close();
fileName = frameWindow.document.getElementById("apiiuFile");
var fileSubmit = frameWindow.document.getElementById("apiiuSubmit");
fileName.onchange = function (e) {
if (e && e.target && e.target.files) {
var nError = ValidateUploadImage(e.target.files);
if (c_oAscServerError.NoError != nError) {
callbackOld(nError);
return;
}
}
callbackOld(c_oAscServerError.NoError);
fileSubmit.click();
};
}
//todo пересмотреть opera //todo пересмотреть opera
if (AscBrowser.isOpera) if (AscBrowser.isOpera)
...@@ -645,7 +658,7 @@ function ShowImageFileDialog (documentId, documentUserId, callback) { ...@@ -645,7 +658,7 @@ function ShowImageFileDialog (documentId, documentUserId, callback) {
fileName.click(); fileName.click();
} }
function InitDragAndDrop (oHtmlElement, callback) { function InitDragAndDrop (oHtmlElement, callback) {
if ("undefined" != typeof(FileReader) && "undefined" != typeof(FormData) && null != oHtmlElement) { if ("undefined" != typeof(FileReader) && null != oHtmlElement) {
oHtmlElement["ondragover"] = function (e) { oHtmlElement["ondragover"] = function (e) {
e.preventDefault(); e.preventDefault();
e.dataTransfer.dropEffect = CanDropFiles(e) ? 'copy' : 'none'; e.dataTransfer.dropEffect = CanDropFiles(e) ? 'copy' : 'none';
...@@ -655,34 +668,35 @@ function InitDragAndDrop (oHtmlElement, callback) { ...@@ -655,34 +668,35 @@ function InitDragAndDrop (oHtmlElement, callback) {
e.preventDefault(); e.preventDefault();
var files = e.dataTransfer.files; var files = e.dataTransfer.files;
var nError = ValidateUploadImage(files); var nError = ValidateUploadImage(files);
if (c_oAscServerError.NoError !== nError) { callback(nError, files);
callback(g_fMapAscServerErrorToAscError(nError));
return;
}
callback(c_oAscServerError.NoError, files);
}; };
} }
} }
function UploadImageFiles (files, documentId, documentUserId, callback) { function UploadImageFiles (files, documentId, documentUserId, callback) {
var xhr = new XMLHttpRequest(); if (files.length > 0) {
var fd = new FormData(); var xhr = new XMLHttpRequest();
for(var i = 0, length = files.length; i < length; i++) xhr.open('POST', g_sUploadServiceLocalUrl + '/' + documentId + '/' + documentUserId + '/' + g_oDocumentUrls.getMaxIndex());
fd.append('file[' + i + ']', files[i]); xhr.onreadystatechange = function() {
xhr.open('POST', g_sUploadServiceLocalUrl + '/' + documentId + '/' + documentUserId + '/' + g_oDocumentUrls.getMaxIndex()); if (4 == this.readyState) {
xhr.onreadystatechange = function() { if((this.status == 200 || this.status == 1223)) {
if (4 == this.readyState) { var urls = JSON.parse(this.responseText);
if((this.status == 200 || this.status == 1223)) { g_oDocumentUrls.addUrls(urls);
var frameWindow = GetUploadIFrame(); var firstUrl;
var content = this.responseText; for (var i in urls) {
frameWindow.document.open(); if (urls.hasOwnProperty(i)) {
frameWindow.document.write(content); firstUrl = urls[i];
frameWindow.document.close(); break;
} else }
callback(c_oAscError.ID.Unknown); }
} callback(c_oAscServerError.NoError, firstUrl);
}; } else
xhr.send(fd); callback(c_oAscError.ID.Unknown);
}
};
xhr.send(files[0]);
} else {
callback(c_oAscError.ID.Unknown);
}
} }
function ValidateUploadImage( files ) { function ValidateUploadImage( files ) {
var nRes = c_oAscServerError.NoError; var nRes = c_oAscServerError.NoError;
...@@ -759,6 +773,23 @@ function GetUploadIFrame() { ...@@ -759,6 +773,23 @@ function GetUploadIFrame() {
} }
return window.frames[sIFrameName]; return window.frames[sIFrameName];
} }
function GetUploadInput(onchange) {
var inputName = 'apiiuFile';
var input = document.getElementById( inputName );
//удаляем чтобы очистить input от предыдущего ввода
if (input) {
document.body.removeChild(input);
}
input = document.createElement("input");
input.setAttribute('id', inputName);
input.setAttribute('name', inputName);
input.setAttribute('type', 'file');
input.setAttribute('accept', 'image/*');
input.setAttribute('style', 'position:absolute;left:-2px;top:-2px;width:1px;height:1px;z-index:-1000;');
input.onchange = onchange;
document.body.appendChild( input );
return input;
}
var arrayRowSeparatorDef = ";", arrayColSeparatorDef = ",", digitSeparatorDef = ".", functionArgumentSeparatorDef = ",", var arrayRowSeparatorDef = ";", arrayColSeparatorDef = ",", digitSeparatorDef = ".", functionArgumentSeparatorDef = ",",
arrayRowSeparator = ";", arrayColSeparator = ",", digitSeparator = ".", functionArgumentSeparator = ","; arrayRowSeparator = ";", arrayColSeparator = ",", digitSeparator = ".", functionArgumentSeparator = ",";
......
...@@ -178,31 +178,14 @@ var ASC_DOCS_API_USE_EMBEDDED_FONTS = "@@ASC_DOCS_API_USE_EMBEDDED_FONTS"; ...@@ -178,31 +178,14 @@ var ASC_DOCS_API_USE_EMBEDDED_FONTS = "@@ASC_DOCS_API_USE_EMBEDDED_FONTS";
if (c_oAscServerError.NoError !== error) { if (c_oAscServerError.NoError !== error) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical); t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
} else { } else {
var ws = t.wb.getWorksheet(); t._addImageUrl(url);
if (ws) {
if (t.isImageChangeUrl || t.isShapeImageChangeUrl || t.isTextArtChangeUrl) {
ws.objectRender.editImageDrawingObject(url);
} else {
ws.objectRender.addImageDrawingObject(url, null);
}
}
} }
t.handlers.trigger("asc_onEndAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage); t.handlers.trigger("asc_onEndAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}); });
// init drag&drop // init drag&drop
InitDragAndDrop(this.HtmlElement, function(error, files) { InitDragAndDrop(this.HtmlElement, function(error, files) {
if (c_oAscServerError.NoError !== error) { t._uploadCallback(error, files);
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
return;
}
t.handlers.trigger("asc_onStartAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
UploadImageFiles(files, t.documentId, t.documentUserId, function(error) {
if (c_oAscServerError.NoError !== error) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
t.handlers.trigger("asc_onEndAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}
});
}); });
this.formulasList = getFormulasInfo(); this.formulasList = getFormulasInfo();
...@@ -2634,14 +2617,41 @@ var ASC_DOCS_API_USE_EMBEDDED_FONTS = "@@ASC_DOCS_API_USE_EMBEDDED_FONTS"; ...@@ -2634,14 +2617,41 @@ var ASC_DOCS_API_USE_EMBEDDED_FONTS = "@@ASC_DOCS_API_USE_EMBEDDED_FONTS";
spreadsheet_api.prototype.asc_showImageFileDialog = function() { spreadsheet_api.prototype.asc_showImageFileDialog = function() {
var t = this; var t = this;
ShowImageFileDialog(this.documentId, this.documentUserId, function(error) { ShowImageFileDialog(this.documentId, this.documentUserId, function(error, files) {
t._uploadCallback(error, files);
}, function(error) {
if (c_oAscServerError.NoError !== error) { if (c_oAscServerError.NoError !== error) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical); t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
} }
t.handlers.trigger("asc_onStartAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage); t.handlers.trigger("asc_onStartAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}); });
}; };
spreadsheet_api.prototype._uploadCallback = function(error, files) {
var t = this;
if (c_oAscServerError.NoError !== error) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
} else {
t.handlers.trigger("asc_onStartAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
UploadImageFiles(files, t.documentId, t.documentUserId, function(error, url) {
if (c_oAscServerError.NoError !== error) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
} else {
t._addImageUrl(url);
}
t.handlers.trigger("asc_onEndAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
});
}
};
spreadsheet_api.prototype._addImageUrl = function(url) {
var ws = this.wb.getWorksheet();
if (ws) {
if (this.isImageChangeUrl || this.isShapeImageChangeUrl || this.isTextArtChangeUrl) {
ws.objectRender.editImageDrawingObject(url);
} else {
ws.objectRender.addImageDrawingObject(url, null);
}
}
};
spreadsheet_api.prototype.asc_setSelectedDrawingObjectLayer = function(layerType) { spreadsheet_api.prototype.asc_setSelectedDrawingObjectLayer = function(layerType) {
var ws = this.wb.getWorksheet(); var ws = this.wb.getWorksheet();
return ws.objectRender.setGraphicObjectLayer(layerType); return ws.objectRender.setGraphicObjectLayer(layerType);
......
...@@ -187,17 +187,7 @@ function asc_docs_api(name) ...@@ -187,17 +187,7 @@ function asc_docs_api(name)
}); });
// init drag&drop // init drag&drop
InitDragAndDrop(document.getElementById(this.HtmlElementName), function (error, files) { InitDragAndDrop(document.getElementById(this.HtmlElementName), function (error, files) {
if (c_oAscServerError.NoError !== error) { oThis._uploadCallback(error, files);
oThis.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical);
return;
}
oThis.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
UploadImageFiles(files, documentId, documentUserId, function (error) {
if (c_oAscServerError.NoError !== error) {
oThis.asc_fireCallback("asc_onError", c_oAscError.ID.Unknown, c_oAscError.Level.NoCritical);
oThis.sync_EndAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}
});
}); });
if (window.editor == undefined) if (window.editor == undefined)
...@@ -3017,13 +3007,29 @@ asc_docs_api.prototype.ChangeArtImageFromFile = function() ...@@ -3017,13 +3007,29 @@ asc_docs_api.prototype.ChangeArtImageFromFile = function()
asc_docs_api.prototype.AddImage = function(){ asc_docs_api.prototype.AddImage = function(){
var t = this; var t = this;
ShowImageFileDialog(documentId, documentUserId, function (error) { ShowImageFileDialog(documentId, documentUserId, function (error, files) {
t._uploadCallback(error, files);
}, function (error) {
if (c_oAscServerError.NoError !== error) if (c_oAscServerError.NoError !== error)
t.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical); t.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical);
t.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage); t.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}); });
}; };
asc_docs_api.prototype._uploadCallback = function(error, files) {
var t =this;
if (c_oAscServerError.NoError !== error) {
t.sync_ErrorCallback(error, c_oAscError.Level.NoCritical);
} else {
t.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
UploadImageFiles(files, documentId, documentUserId, function (error, url) {
if (c_oAscServerError.NoError !== error)
t.sync_ErrorCallback(error, c_oAscError.Level.NoCritical);
else
t.AddImageUrl(url);
t.sync_EndAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
});
}
}
asc_docs_api.prototype.StartAddShape = function(prst, is_apply) asc_docs_api.prototype.StartAddShape = function(prst, is_apply)
{ {
this.WordControl.m_oLogicDocument.StartAddShape(prst, is_apply); this.WordControl.m_oLogicDocument.StartAddShape(prst, is_apply);
......
...@@ -537,17 +537,7 @@ function asc_docs_api(name) ...@@ -537,17 +537,7 @@ function asc_docs_api(name)
}); });
// init drag&drop // init drag&drop
InitDragAndDrop(document.getElementById(this.HtmlElementName), function (error, files) { InitDragAndDrop(document.getElementById(this.HtmlElementName), function (error, files) {
if (c_oAscServerError.NoError !== error) { oThis._uploadCallback(error, files);
oThis.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical);
return;
}
oThis.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
UploadImageFiles(files, documentId, documentUserId, function (error) {
if (c_oAscServerError.NoError !== error) {
oThis.asc_fireCallback("asc_onError", c_oAscError.ID.Unknown, c_oAscError.Level.NoCritical);
oThis.sync_EndAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}
});
}); });
if (window.editor == undefined) if (window.editor == undefined)
...@@ -4717,13 +4707,29 @@ asc_docs_api.prototype.ChangeShapeImageFromFile = function() ...@@ -4717,13 +4707,29 @@ asc_docs_api.prototype.ChangeShapeImageFromFile = function()
asc_docs_api.prototype.AddImage = function(){ asc_docs_api.prototype.AddImage = function(){
var t = this; var t = this;
ShowImageFileDialog(documentId, documentUserId, function (error) { ShowImageFileDialog(documentId, documentUserId, function(error, files){
t._uploadCallback(error, files);
}, function (error) {
if (c_oAscServerError.NoError !== error) if (c_oAscServerError.NoError !== error)
t.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical); t.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical);
t.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage); t.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
}); });
}; };
asc_docs_api.prototype._uploadCallback = function(error, files){
var t = this;
if (c_oAscServerError.NoError !== error) {
t.sync_ErrorCallback(error, c_oAscError.Level.NoCritical);
} else {
t.sync_StartAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
UploadImageFiles(files, documentId, documentUserId, function (error, url) {
if (c_oAscServerError.NoError !== error)
t.sync_ErrorCallback(error, c_oAscError.Level.NoCritical);
else
t.AddImageUrl(url);
t.sync_EndAction(c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
});
}
}
asc_docs_api.prototype.AddImageUrl2 = function(url) asc_docs_api.prototype.AddImageUrl2 = function(url)
{ {
this.AddImageUrl(getFullImageSrc2(url)); this.AddImageUrl(getFullImageSrc2(url));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment