Ajax cannot submit files. Therefore, when uploading and previewing images, we often use Ifame to achieve seemingly asynchronous results. However, this is not always very convenient, AjaxFilleUpload. js wraps the above method so that we do not need to manage a series of Iframe operations or affect our page structure to implement asynchronous file submission.
Html:
Copy codeThe Code is as follows:
<Input type = "file" name = "upload" hidden = "hidden" id = "file_upload" accept = ". zip"/>
Js:
Copy codeThe Code is as follows:
$. AjaxFileUpload ({
Url: '$ {pageContext. request. contextPath}/Manage/BR_restorePic.action', // link to the server address
Secureuri: false,
FileElementId: 'file _ upload', // id attribute of the file selection box
DataType: 'text', // format returned by the server, which can be json or xml
Success: function (data, status) // equivalent to the usage of the try statement block in java
{
Certificate ('{restoredialog'}.html (data );
// Alert (data );
},
Error: function (data, status, e) {// equivalent to the catch Block usage in java
Upload ('{restoredialog'{.html ("Upload Failed, please try again ");
}
});
This method also has a problem, that is, the input can only be used once, and the second onchange of the input will not be executed, which should be related to the browser. The solution is to replace the input
Like this:
Copy codeThe Code is as follows:
$ ('# File_upload '). replaceWith ('<input type = "file" name = "upload" hidden = "hidden" id = "file_upload" accept = ". zip "/> ');