Answered by:
Multiple File Upload

Question
-
User226796895 posted
I got this from a tutorial video and I can't figure out where my problem is. Uploading a single file works fine. Just the button to add another file field doesn't want to work out for me yet. Could someone please point out my error? spelling mistake? The tutorial link is... http://www.asp.net/web-forms/videos/how-do-i/how-do-i-multiple-file-uploads-in-aspnet-1
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="FormFileUpload" runat="server"> <b style="font-weight: bold">Multi File Upload<br /> <br /> </b> <div> Please choose the file(s) that you would like to upload.<br /> <p id="upload-area"> <asp:FileUpload ID="FileField" runat="server" Width="360px"/> </p> <input id="ButtonAdd" type="button" value="Add a File" onclick="addFileUploadBox" /> <asp:Button ID="UploadButton" runat="server" Text="Click to process your Upload" /> </div> <div> </div> <span id="Span1" style="color:red" runat="server" /> <script type="text/javascript"> function addFileUploadBox() { if (!document.getElementById || !document.createElement) return false var uploadArea = document.getElementById("upload-area"); if (!uploadArea) return; var NewLine = document.createElement("br"); uploadArea.appendchild(NewLine); var newUploadBox = document.createElement("input"); newUploadBox.type = "file"; newUploadBox.size = "360px"; if (!addFileUploadBox.lastassignedId) addFileUploadBox.lastassignedId = 100; newUploadBox.setAttribute("Id", "FileField" + addFileUploadBox.lastassignedId); newUploadBox.setAttribute("Name", "FileField" + addFileUploadBox.lastassignedId); uploadArea.appendchild(newUploadBox); addFileUploadBox.lastassignedId++; } </script> </form> </body> </html>
Partial Class Default2 Inherits System.Web.UI.Page Dim FileField1 As Object Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim Uppath As String Dim Upname As String Uppath = "C:\Users\Trevor\Desktop\File Upload\UploadedFiles\" Upname = Dir(Uppath, vbDirectory) If (Upname = "") Then MkDir("C:\Users\Trevor\Desktop\File Upload\UploadedFiles\") End If End Sub Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click Dim uploads As HttpFileCollection uploads = HttpContext.Current.Request.Files For i As Integer = 0 To (uploads.Count - 1) If (uploads(i).ContentLength > 0) Then Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName) Try FileField.PostedFile.SaveAs("C:\Users\Trevor\Desktop\File Upload\UploadedFiles\" + c) Span1.InnerHtml = "File Upload Successful." Catch ex As Exception Span1.InnerHtml = "File Upload FAILED." End Try End If Next i End Sub End Class
Wednesday, January 8, 2014 11:16 PM
Answers
-
User1176121428 posted
Hi TrevorReimer,
thanks for your post.
According to your description,I test your code.
You should change
newUploadBox.size = "360px";
to
newUploadBox.size = "360"; // I suggest that you'd better change newUploadBox.size = "60";that looks fine
It will work.
Hope this helps you.Best Regards,
Eileen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 9, 2014 10:07 PM -
User1176121428 posted
Hi Tre,
You can try to change
Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click Dim uploads As HttpFileCollection uploads = HttpContext.Current.Request.Files For i As Integer = 0 To (uploads.Count - 1) If (uploads(i).ContentLength > 0) Then Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName) Try FileField.PostedFile.SaveAs("C:\Users\Trevor\Desktop\File Upload\UploadedFiles\" + c) Span1.InnerHtml = "File Upload Successful." Catch ex As Exception Span1.InnerHtml = "File Upload FAILED." End Try End If Next i End Sub End Class
to
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Dim FileName As String Dim uploads As HttpFileCollection = HttpContext.Current.Request.Files For i As Integer = 0 To (uploads.Count - 1) Dim FileToUpload As HttpPostedFile = uploads(i) If (FileToUpload.ContentLength = 0) Then ' Error Handling Else FileName = System.IO.Path.GetFileName(FileToUpload.FileName) Try FileToUpload.SaveAs("C:\Users\Trevor\Desktop\File Upload\UploadedFiles\" + FileName) Span1.InnerHtml = "File Upload Successful." Catch ex As Exception Span1.InnerHtml = "File Upload FAILED." End Try End If Next i End Sub
Hope this helps you.
Best Regards,Eileen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 14, 2014 12:23 AM
All replies
-
User-111351935 posted
hi,
Use Below link for multiple file upload.
http://www.aspdotnet-suresh.com/2012/12/aspnet-upload-multiple-files-using.html
Wednesday, January 8, 2014 11:36 PM -
User226796895 posted
Can anyone tell me where I made the mistake? I really think that I made a simple mistake somewhere that it doesn't work. But thanks "rajeshsomvan" for the above link. I'll try that yet.
Thursday, January 9, 2014 2:44 PM -
User1176121428 posted
Hi TrevorReimer,
thanks for your post.
According to your description,I test your code.
You should change
newUploadBox.size = "360px";
to
newUploadBox.size = "360"; // I suggest that you'd better change newUploadBox.size = "60";that looks fine
It will work.
Hope this helps you.Best Regards,
Eileen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 9, 2014 10:07 PM -
User226796895 posted
For some reason when I try it it doesn't seem to make a difference. I must have some other error. Thanks Eilen. One more thing is if I set the UploadBox.size = "60" then there isn't even enough space for the browse button. I don't know why because i have seen that before in some code but I don't seem to have luck with that. Thanks.
Friday, January 10, 2014 6:23 PM -
User1176121428 posted
Hi TrevorReimer,
Based on your reply,Please show error shotscreen for us.
And then I suggest that you can consider download the complete code to test.It works.
Best Regards,
Eileen
Sunday, January 12, 2014 9:19 PM -
User226796895 posted
The thing is I don't get any error. Even if I alter the java to insert a new Upload box. Everything works fine except for that the Upload fields don't populate the way they should. I''l try the whole tutorial video again tomorrow and see what I get then.
Thanks for your effort.
Trevor
Sunday, January 12, 2014 10:59 PM -
User1176121428 posted
Hi Tre,
You can try to change
Protected Sub UploadButton_Click(sender As Object, e As EventArgs) Handles UploadButton.Click Dim uploads As HttpFileCollection uploads = HttpContext.Current.Request.Files For i As Integer = 0 To (uploads.Count - 1) If (uploads(i).ContentLength > 0) Then Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName) Try FileField.PostedFile.SaveAs("C:\Users\Trevor\Desktop\File Upload\UploadedFiles\" + c) Span1.InnerHtml = "File Upload Successful." Catch ex As Exception Span1.InnerHtml = "File Upload FAILED." End Try End If Next i End Sub End Class
to
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click Dim FileName As String Dim uploads As HttpFileCollection = HttpContext.Current.Request.Files For i As Integer = 0 To (uploads.Count - 1) Dim FileToUpload As HttpPostedFile = uploads(i) If (FileToUpload.ContentLength = 0) Then ' Error Handling Else FileName = System.IO.Path.GetFileName(FileToUpload.FileName) Try FileToUpload.SaveAs("C:\Users\Trevor\Desktop\File Upload\UploadedFiles\" + FileName) Span1.InnerHtml = "File Upload Successful." Catch ex As Exception Span1.InnerHtml = "File Upload FAILED." End Try End If Next i End Sub
Hope this helps you.
Best Regards,Eileen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 14, 2014 12:23 AM -
User226796895 posted
Thanks Eileen. It works!
Tuesday, January 14, 2014 2:35 PM