问题
I want to upload file by using dropzone. First I will check this file format when user upload his/her file, so I guess I need to set "autoProcessQueue: true" so that the uploaded file will be checked automatically. If the file format is not right, user will be asked to upload again until upload right file.
Then when user click "save" button, this file will be uploaded into my server.
My javascript code is below:
Dropzone.options.myDropzone = {
autoProcessQueue: true,
addRemoveLinks: true,
maxFilesize: 2, // MB
acceptedFiles: ".xlsx",
maxFiles: 1,
url: 'MyPage.aspx?File=<%=Request.QueryString["File"]%>',
init: function () {
this.on("addedfile", function () {
if (this.files[1] != null) {
this.removeFile(this.files[0]);
}
});
$('#_btnTest').click(function () {
alert('aaa')
myDropzone.processQueue();
alert('bbb')
});
},
};
My aspx code
<div id="myDropzone" class="dropzone">
<div class="fallback">
<input name="file" type="file" runat="server" />
</div>
</div>
<asp:Button ID="_btnSave" runat="server" Text="Save" class="btn btn-info btn-sm" OnClick="_btnSave_Click" />
My C# code below
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["File"] != null)
{
CheckFormat();
}
}
When I upload file it will run "CheckFormat()" method and check automatically. But my problem is that when I click "save" button will not run Page_Load() method, so I can't get the file in code-behind, this problem let me can not upload file when click "save" button. By the way, I guess "myDropzone.processQueue();" is invalid.
what is your solution about this case? How to do when you want to check file first then save this file to server when user click button? Please help me and thank you so much.
回答1:
When your first upload request finished, file status will become "success", in which case, this file will be skipped when you submit upload request again.
Solution: Change file status to "queued" so it will be upload again. Something like following:
this.on("addedfile", function (file) {
if (this.files[1] != null) {
this.removeFile(this.files[0]);
} else {
file.status = 'queued';
}
});
来源:https://stackoverflow.com/questions/32389573/using-dropzone-to-upload-twice-with-asp-net