I have a <asp:FileUpload> with the AllowMultiple property set to true:
<asp:fileupload id="FileUpload" runat="server" allowmultiple="true" cssclass="fileUpload btn btn-sm btn-default" onchange="preloadImages()" /> <div class="field col-md-2 col-md-offset-1 immScarpePreview MainPreviewBox"> <asp:image id="Image1" runat="server" visible="false" cssclass="img-responsive" /> <asp:button id="ImmButton" runat="server" text="Upload" onclick="ImmButton_Click" cssclass="hidden" /> </div>
In my JavaScript function, I simulate a click on an invisible button:
<script> function preloadImages() { $('#<%=ImmButton.ClientID%>').trigger('click'); } </script>
In the code, I save the files to a temporary folder, and I show the downloaded images in the <asp:Image> controls:
protected void ImmButton_Click(object sender, EventArgs e) { if (FileUpload.HasFile) { try { int cont = 0; byte[] fileData = null; foreach (HttpPostedFile file in FileUpload.PostedFiles) { if (cont == 0) { using (var binaryReader = new BinaryReader(file.InputStream)) fileData = binaryReader.ReadBytes(file.ContentLength); File.WriteAllBytes(Server.MapPath("immScarpe/tmp/" + file.FileName), fileData); setImage1("immScarpe/tmp/" + file.FileName); } else if (cont == 1) { using (var binaryReader = new BinaryReader(file.InputStream)) fileData = binaryReader.ReadBytes(file.ContentLength); File.WriteAllBytes(Server.MapPath("immScarpe/tmp/" + file.FileName), fileData); setImage2("immScarpe/tmp/" + file.FileName); } else if (cont == 2)
This works fine, but I need help. When I go through FileUpload.PostedFiles, the order of the selected images is in alphabetical order, I think. I would like to keep the user selection order. Is it possible?
Martina
source share