0
我使用Plupload上傳文件到一個服務器,上傳過程效果很好,但是當我打開文件(例如:docx)時,它說它已損壞,並有其他數據我認爲這是來自xmlhttprequest的一些標題)。plupload jquery插件 - 上傳後損壞的文件
你可以看到在這個環節的附加數據 - >http://prntscr.com/4hl5lo
我的代碼:
客戶端:
AJS.$("input#edit-template-file").replaceWith('<div id="container"> <a id="pickfiles" href="javascript:;">[Select files]</a> <a id="uploadfiles" href="javascript:;">[Upload files]</a> </div>');
var uploader = new plupload.Uploader({
runtimes : 'html5,flash,silverlight,html4',
browse_button : 'pickfiles', // you can pass in id...
container: document.getElementById('container'), // ... or DOM Element itself
url : '[my url]',
multi_selection: false,
send_file_name: false,
chunk_size: 0,
filters : {
max_file_size : '100mb',
mime_types: [
{title : "Doc files", extensions : "docx,docm,dotx,dotm,odt,rtf"}
]
},
// Flash settings
flash_swf_url : 'Moxie.swf',
// Silverlight settings
silverlight_xap_url : 'Moxie.xap',
init: {
PostInit: function() {
document.getElementById('uploadfiles').onclick = function() {
uploader.start();
return false;
};
},
BeforeUpload: function(up, file) {
// Called right before the upload for a given file starts, can be used to cancel it if required
delete up.settings.multipart_params; //i have tried this to remove the additional data written in the file but without sucess
console.log('[BeforeUpload]', 'File: ', file);
},
UploadProgress: function(up, file) {
AJS.progressBars.update("#templateBar", file.percent);
},
FileUploaded: function(up, file, info) {
console.log(info.response);
if (info.response != '') {
AJS.$(fileFieldId).val(info.response);
AJS.$("div#container").attr('name', file.name);
setSubmitState(false);
}
},
Error: function(up, err) {
document.getElementById('console').innerHTML += "\nError #" + err.code + ": " + err.message;
}
}
});
uploader.init();
服務器端(JAVA):
public Response createFile(InputStream is) throws IOException {
final String templatesPath = buildTemplatesPath();
final File templatesDir = new File(templatesPath);
if (!templatesDir.exists()) {
templatesDir.mkdir();
}
final String tmpPath = buildTMPPath();
final File tmpDir = new File(tmpPath);
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
final String fileId = UUID.randomUUID().toString();
try {
writeToFile(is, buildFilePath(fileId));
} catch (Exception e) {
logger.error("Unable to write template", e);
return Response.status(500);
}
return Response.status(200);
}
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) throws Exception {
try {
final OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (Exception e) {
logger.error("Unable write file", e);
throw e;
}
}
我一直在研究這個主題,並發現Plupload寫入的文件在xmlhttprequest(https://github.com/moxiecode/moxie/blob/master/src/flash/src/com/XMLHttpRequest.as)中額外的頭文件,但我不能刪除該數據,任何幫助?
對不起Ankit我不明白你想解釋什麼。上傳後的文件已損壞,您可以在此鏈接http://prntscr.com/4hlkj6中看到。 – 2014-08-29 10:38:46