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

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
OnError_FixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
0
CJ_SCommented:
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
nauman_ahmedCommented:
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

OnError_FixAuthor Commented:
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
OnError_FixAuthor Commented:
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
mmarinovCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OnError_FixAuthor Commented:
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
mmarinovCommented:
ohhhhhhhhhhh, this is vb

this should be the correct code

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

B..M
mmarinov
0
OnError_FixAuthor Commented:
Thanks mmarinov...
Enjoy your points!
0
mmarinovCommented:
Glad to be in help OnError

B..M
mmarinov
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.