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') {
var g_oZipChanges = null;
var g_sDownloadServiceLocalUrl = "/downloadas";
var g_sUploadServiceLocalUrl = "/upload";
var g_sUploadServiceLocalUrlOld = "/uploadold";
var g_nMaxRequestLength = 5242880;//5mb <requestLimits maxAllowedContentLength="30000000" /> default 30mb
var g_cCharDelimiter = String.fromCharCode(5);
......@@ -616,27 +617,39 @@ function InitOnMessage (callback) {
}, false);
}
}
function ShowImageFileDialog (documentId, documentUserId, callback) {
var frameWindow = GetUploadIFrame();
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>';
frameWindow.document.open();
frameWindow.document.write(content);
frameWindow.document.close();
var 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) {
callback(g_fMapAscServerErrorToAscError(nError));
return;
function ShowImageFileDialog (documentId, documentUserId, callback, callbackOld) {
var fileName;
if ("undefined" != typeof(FileReader)) {
fileName = GetUploadInput(function (e) {
if (e && e.target && e.target.files) {
var nError = ValidateUploadImage(e.target.files);
callback(nError, e.target.files);
} else {
callback(c_oAscServerError.Unknown);
}
}
callback(c_oAscServerError.NoError);
fileSubmit.click();
};
});
} else {
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
if (AscBrowser.isOpera)
......@@ -645,7 +658,7 @@ function ShowImageFileDialog (documentId, documentUserId, callback) {
fileName.click();
}
function InitDragAndDrop (oHtmlElement, callback) {
if ("undefined" != typeof(FileReader) && "undefined" != typeof(FormData) && null != oHtmlElement) {
if ("undefined" != typeof(FileReader) && null != oHtmlElement) {
oHtmlElement["ondragover"] = function (e) {
e.preventDefault();
e.dataTransfer.dropEffect = CanDropFiles(e) ? 'copy' : 'none';
......@@ -655,34 +668,35 @@ function InitDragAndDrop (oHtmlElement, callback) {
e.preventDefault();
var files = e.dataTransfer.files;
var nError = ValidateUploadImage(files);
if (c_oAscServerError.NoError !== nError) {
callback(g_fMapAscServerErrorToAscError(nError));
return;
}
callback(c_oAscServerError.NoError, files);
callback(nError, files);
};
}
}
function UploadImageFiles (files, documentId, documentUserId, callback) {
var xhr = new XMLHttpRequest();
var fd = new FormData();
for(var i = 0, length = files.length; i < length; i++)
fd.append('file[' + i + ']', files[i]);
xhr.open('POST', g_sUploadServiceLocalUrl + '/' + documentId + '/' + documentUserId + '/' + g_oDocumentUrls.getMaxIndex());
xhr.onreadystatechange = function() {
if (4 == this.readyState) {
if((this.status == 200 || this.status == 1223)) {
var frameWindow = GetUploadIFrame();
var content = this.responseText;
frameWindow.document.open();
frameWindow.document.write(content);
frameWindow.document.close();
} else
callback(c_oAscError.ID.Unknown);
}
};
xhr.send(fd);
if (files.length > 0) {
var xhr = new XMLHttpRequest();
xhr.open('POST', g_sUploadServiceLocalUrl + '/' + documentId + '/' + documentUserId + '/' + g_oDocumentUrls.getMaxIndex());
xhr.onreadystatechange = function() {
if (4 == this.readyState) {
if((this.status == 200 || this.status == 1223)) {
var urls = JSON.parse(this.responseText);
g_oDocumentUrls.addUrls(urls);
var firstUrl;
for (var i in urls) {
if (urls.hasOwnProperty(i)) {
firstUrl = urls[i];
break;
}
}
callback(c_oAscServerError.NoError, firstUrl);
} else
callback(c_oAscError.ID.Unknown);
}
};
xhr.send(files[0]);
} else {
callback(c_oAscError.ID.Unknown);
}
}
function ValidateUploadImage( files ) {
var nRes = c_oAscServerError.NoError;
......@@ -759,6 +773,23 @@ function GetUploadIFrame() {
}
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 = ",",
arrayRowSeparator = ";", arrayColSeparator = ",", digitSeparator = ".", functionArgumentSeparator = ",";
......
......@@ -178,31 +178,14 @@ var ASC_DOCS_API_USE_EMBEDDED_FONTS = "@@ASC_DOCS_API_USE_EMBEDDED_FONTS";
if (c_oAscServerError.NoError !== error) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
} else {
var ws = t.wb.getWorksheet();
if (ws) {
if (t.isImageChangeUrl || t.isShapeImageChangeUrl || t.isTextArtChangeUrl) {
ws.objectRender.editImageDrawingObject(url);
} else {
ws.objectRender.addImageDrawingObject(url, null);
}
}
t._addImageUrl(url);
}
t.handlers.trigger("asc_onEndAction", c_oAscAsyncActionType.BlockInteraction, c_oAscAsyncAction.UploadImage);
});
// init drag&drop
InitDragAndDrop(this.HtmlElement, function(error, files) {
if (c_oAscServerError.NoError !== error) {
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);
}
});
t._uploadCallback(error, files);
});
this.formulasList = getFormulasInfo();
......@@ -2634,14 +2617,41 @@ var ASC_DOCS_API_USE_EMBEDDED_FONTS = "@@ASC_DOCS_API_USE_EMBEDDED_FONTS";
spreadsheet_api.prototype.asc_showImageFileDialog = function() {
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) {
t.handlers.trigger("asc_onError", error, c_oAscError.Level.NoCritical);
}
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) {
var ws = this.wb.getWorksheet();
return ws.objectRender.setGraphicObjectLayer(layerType);
......
......@@ -187,17 +187,7 @@ function asc_docs_api(name)
});
// init drag&drop
InitDragAndDrop(document.getElementById(this.HtmlElementName), function (error, files) {
if (c_oAscServerError.NoError !== error) {
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);
}
});
oThis._uploadCallback(error, files);
});
if (window.editor == undefined)
......@@ -3017,13 +3007,29 @@ asc_docs_api.prototype.ChangeArtImageFromFile = function()
asc_docs_api.prototype.AddImage = function(){
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)
t.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical);
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)
{
this.WordControl.m_oLogicDocument.StartAddShape(prst, is_apply);
......
......@@ -537,17 +537,7 @@ function asc_docs_api(name)
});
// init drag&drop
InitDragAndDrop(document.getElementById(this.HtmlElementName), function (error, files) {
if (c_oAscServerError.NoError !== error) {
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);
}
});
oThis._uploadCallback(error, files);
});
if (window.editor == undefined)
......@@ -4717,13 +4707,29 @@ asc_docs_api.prototype.ChangeShapeImageFromFile = function()
asc_docs_api.prototype.AddImage = function(){
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)
t.asc_fireCallback("asc_onError", error, c_oAscError.Level.NoCritical);
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)
{
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