?
Solved

Retrieving multiple files through <input type="file"> via form post

Posted on 2004-11-17
10
Medium Priority
?
1,743 Views
Last Modified: 2007-12-19
Hi Experts,

One of my web forms is dynamically generated using data from an XML file. This web form displays a number of "Upload File" (<input type='file'>) boxes depending on the XML file given.

Now the problem is -- how on Earth do I then iterate through the Request object to deal (firstly) with only the type=file boxes, and then the other data?

I do still need to extract the filename and (preferrably) also the filesize of each of the files coming in.

Thanks,

OnError_Fix
0
Comment
Question by:OnError_Fix
10 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12602928
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12603037
You can iterate through the Request.Files. This is a collection which consists of HttpPostedFile objects. This HttpPostFile object has a SaveAsmethod.

Dim hpf as HttpPostedFile
For each hpf In Request.Files
  hpf.SaveAs(String.Concat(Server.MapPath("/"), hpf.FileName.SubString(hpf.FileName.lastIndexOf("\")+1, hpf.FileName.Length-hpf.FileName.lastIndexOf("\")-1)
Next
0
 
LVL 25

Expert Comment

by:nauman_ahmed
ID: 12604389
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:OnError_Fix
ID: 12612718
Hi experts,

Thanks for your contributions -- apologies I have been a bit tardy getting back to you. I will review the answers you've all given within the next few hours and post back here with my findings.

Thanks,

OnError_Fix
0
 

Author Comment

by:OnError_Fix
ID: 12613382
Ok experts....

Working with mmarinov's C# implementation (I'm converting to VB.NET by the way), I have managed to figure out most of what I need to know. The next part is how do I chuck the contents of the requested file into an SQL Image parameter?

EG > cmdNew.Parameters("@Data").Value = objRqst.Files(i).InputStream

That line actually generates an error, saying:

System.InvalidCastException: Object must implement IConvertible. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Incorpex.Olympia2004V1.IPublish_V1.SaveDocumentToXML(HttpRequest objRqst, Int32 TemplateID, Int32 CategoryID) in C:\Documents and Settings\r.parker\VSWebCache\intranet.domain.net\resources\ipublish\IPublish_V1.vb:line 103

So any ideas?

I've increased the points value to reflect the fact I've asked something else that wasn't in the original post.

Thanks,

OnError_Fix
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 1400 total points
ID: 12613977
you have ti get the file in byte array
try this

byte[] buffer = new byte[s.Length];
s.Read(buffer, 0, (int)s.Length);

Dim buffer() as Byte = New Byte(objRqst.Files(i).Length)
objRqst.Files(i).Read ( buffer, 0, CInt(objRqst.Files(i).Length))

cmdNew.Parameters("@Data").Value = buffer

B..M
mmarinov
0
 

Author Comment

by:OnError_Fix
ID: 12614566
mmarinov,

here's the latest I'm using:

Dim arrFile(objRqst.Files(i).ContentLength) As Byte
Dim objStream As Stream

objStream = objRqst.Files(i).InputStream
objStream.Read(arrFile, 0, objRqst.Files(i).ContentLength)

cmdNew.Parameters("@Data").Value = arrFile

But it still throws an error:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.ArrayList.get_Item(Int32 index) at System.Collections.Specialized.NameObjectCollectionBase.BaseGet(Int32 index) at System.Web.HttpFileCollection.Get(Int32 index) at System.Web.HttpFileCollection.get_Item(Int32 index) at Incorpex.Olympia2004V1.IPublish_V1.SaveDocumentToXML(HttpRequest objRqst, Int32 TemplateID, Int32 CategoryID) in C:\Documents and Settings\r.parker\VSWebCache\intranet.domain.net\resources\ipublish\IPublish_V1.vb:line 98

Here's the full code I'm using:

For i = 1 To objRqst.Files.Count

                    objConn.Open()
                    cmdNew.Parameters("@AttachedTo").Value = DocumentID
                    cmdNew.Parameters("@Filename").Value = objRqst.Files(i).FileName
                    cmdNew.Parameters("@Filetype").Value = objRqst.Files(i).ContentType
                    cmdNew.Parameters("@FileSize").Value = objRqst.Files(i).ContentLength
                    cmdNew.Parameters("@UploadedBy").Value = UCase(HttpContext.Current.User.Identity.Name)
                    cmdNew.Parameters("@Created").Value = Now

                    Dim arrFile(objRqst.Files(i).ContentLength) As Byte
                    Dim objStream As Stream

                    objStream = objRqst.Files(i).InputStream
                    objStream.Read(arrFile, 0, objRqst.Files(i).ContentLength)

                    cmdNew.Parameters("@Data").Value = arrFile
                    cmdNew.ExecuteNonQuery()
                    objConn.Close()

Next

Any ideas?
Note: Points increased to 350


0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12614948
ohhhhhhhhhhh, this is vb

this should be the correct code

For i = 1 To objRqst.Files.Count-1

B..M
mmarinov
0
 

Author Comment

by:OnError_Fix
ID: 12615239
Thanks mmarinov...
Enjoy your points!
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12615400
Glad to be in help OnError

B..M
mmarinov
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Loops Section Overview
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question