How do I handle two HTMLFileInput controls on one page?

A

Alan Silver

Hello,

I am writing a page that allows the user to upload two images to the
server. I have two HTMLInputFile controls on the page ...

<input id="smallimage" Type="File" Runat="Server">
<br><input id="largeimage" Type="File" Runat="Server">

and the code that handle the postback looks like this...

if ((smallimage.PostedFile != null) && (smallimage.PostedFile.ContentLength > 0)) {
try {
smallimage.PostedFile.SaveAs(imagePath + "small.jpg");
} catch (Exception ex) {
// display an error message
}
}
if ((largeimage.PostedFile != null) && (largeimage.PostedFile.ContentLength > 0)) {
try {
largeimage.PostedFile.SaveAs(imagePath + "large.jpg");
} catch (Exception ex) {
// display an error message
}
}

Now, if I choose just a small image, then it gets saved twice, once as
"small.jpg" and once as "large.jpg". If I choose both images, then the
small one gets saved twice (ie the large one is ignored). If I only
choose the large image, then it saves something that isn't an image.
Presumably this is the non-existent small image.

I'm sure I'm making some basic mistake here. Please could someone show
me how to handle two file controls on one page.

Thanks in advance.
 
I

intrader

Hello,

I am writing a page that allows the user to upload two images to the
server. I have two HTMLInputFile controls on the page ...

<input id="smallimage" Type="File" Runat="Server">
<br><input id="largeimage" Type="File" Runat="Server">

and the code that handle the postback looks like this...

if ((smallimage.PostedFile != null) && (smallimage.PostedFile.ContentLength > 0)) {
try {
smallimage.PostedFile.SaveAs(imagePath + "small.jpg");
} catch (Exception ex) {
// display an error message
}
}
if ((largeimage.PostedFile != null) && (largeimage.PostedFile.ContentLength > 0)) {
try {
largeimage.PostedFile.SaveAs(imagePath + "large.jpg");
} catch (Exception ex) {
// display an error message
}
}

Now, if I choose just a small image, then it gets saved twice, once as
"small.jpg" and once as "large.jpg". If I choose both images, then the
small one gets saved twice (ie the large one is ignored). If I only
choose the large image, then it saves something that isn't an image.
Presumably this is the non-existent small image.

I'm sure I'm making some basic mistake here. Please could someone show
me how to handle two file controls on one page.

Thanks in advance.
Basically you are making the mistake to assume that web programming is the
the same as forms programming. When you 'choose' one or both of the images
nothing in particular should happen at the server. However, when the web
form is posted your server code is looking at what gets posted back and
the order of the ifs tell the story.
 
A

Alan Silver

Basically you are making the mistake to assume that web programming is the
the same as forms programming. When you 'choose' one or both of the images
nothing in particular should happen at the server. However, when the web
form is posted your server code is looking at what gets posted back and
the order of the ifs tell the story.

Sorry, I don't understand what you mean. The code I showed is executed
on the server when the page is posted back. I was under the impression
that each server control was a separate object in the code.

Therefore, as I have two file input controls, I expected to have two
separate objects in the code. The order of the ifs should be irrelevant
here as each should be independent.

Please could you clarify what you mean and maybe post some code to
explain how you are supposed to handle two file input controls.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top