通过 XHR 上传文件并将流写入文件
Upload file via XHR and write stream to file
我通过 javascript word 插件将 word 2013 docx 发送到 servlet。但是在将流写入文件后,pdf/word(压缩类型)文件已损坏并且大小不同。它有更多字节,然后 js 输出中的 slice.value.size 告诉我(当我将文件保存到硬盘时显示相同的大小)。使用 FormData 和 Apache Fileupload 具有相同的结果。任何帮助将不胜感激。
JS:
function sendDoc(){
Office.context.document.getFileAsync("pdf",
function (result) {
if (result.status == Office.AsyncResultStatus.Succeeded) {
var myFile = result.value;
myFile.getSliceAsync(0, function (slice) {
if (slice.status == Office.AsyncResultStatus.Succeeded) {
showMessage("sending (" + slice.value.size + " bytes)");
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4) {
myFile.closeAsync(function (r) {
if (r.status == Office.AsyncResultStatus.Succeeded) {
showMessage("sent");
}else {
showMessage("error");
}
});
}
}
request.open("POST", "myserver");
request.send(slice.value.data);
}else {
showMessage(slice.status);
}
});
} else {
showMessage(result.status);
}
});
}
Servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
InputStream in = request.getInputStream();
FileOutputStream out = new FileOutputStream("c:/temp/test.pdf");
IOUtils.copy(in, out);
in.close();
out.close();
} catch (Exception e) {
log.error(e.getMessage());
}
}
自己解决了。我必须将 unicode 数字转换为字符并编码 BASE64:
var fileContent = new String();
var docdata = slice.value.data;
for (var j = 0; j < docdata.length; j++) {
fileContent += String.fromCharCode(docdata[j]);
}
var data = btoa(fileContent);
request.send(data);
我通过 javascript word 插件将 word 2013 docx 发送到 servlet。但是在将流写入文件后,pdf/word(压缩类型)文件已损坏并且大小不同。它有更多字节,然后 js 输出中的 slice.value.size 告诉我(当我将文件保存到硬盘时显示相同的大小)。使用 FormData 和 Apache Fileupload 具有相同的结果。任何帮助将不胜感激。
JS:
function sendDoc(){
Office.context.document.getFileAsync("pdf",
function (result) {
if (result.status == Office.AsyncResultStatus.Succeeded) {
var myFile = result.value;
myFile.getSliceAsync(0, function (slice) {
if (slice.status == Office.AsyncResultStatus.Succeeded) {
showMessage("sending (" + slice.value.size + " bytes)");
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4) {
myFile.closeAsync(function (r) {
if (r.status == Office.AsyncResultStatus.Succeeded) {
showMessage("sent");
}else {
showMessage("error");
}
});
}
}
request.open("POST", "myserver");
request.send(slice.value.data);
}else {
showMessage(slice.status);
}
});
} else {
showMessage(result.status);
}
});
}
Servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
InputStream in = request.getInputStream();
FileOutputStream out = new FileOutputStream("c:/temp/test.pdf");
IOUtils.copy(in, out);
in.close();
out.close();
} catch (Exception e) {
log.error(e.getMessage());
}
}
自己解决了。我必须将 unicode 数字转换为字符并编码 BASE64:
var fileContent = new String();
var docdata = slice.value.data;
for (var j = 0; j < docdata.length; j++) {
fileContent += String.fromCharCode(docdata[j]);
}
var data = btoa(fileContent);
request.send(data);