Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2006, 08:36 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default File Upload Question?

I have written an ASP.NET class that generates HTML code. The HTML is returned to the calling form code which then uses response.write to output it to the browser. I need to enable the generated form to upload files to a folder on the server using the type=file input tag:

<input type=file name=filAttachment>

The browse control appears on the generated page, but I seem to be missing whatever is used to save the file on the server when the form is submitted. What do I need to save a file passed back to .NET 1.1 using the HTML input tag?
 
Old October 23rd, 2006, 08:46 AM
Friend of Wrox
Points: 1,935, Level: 17
Points: 1,935, Level: 17 Points: 1,935, Level: 17 Points: 1,935, Level: 17
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2004
Location: United Kingdom
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hey Friend

Its really simple, I can explain it, but better you first visit the link below which has some good explanation, but if u still face problem, feel free to contact:

http://www.c-sharpcorner.com/UploadF...f-c2ad639bcecb

Regards
Mike
 
Old October 23rd, 2006, 08:48 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

To access the tags property in code, add runat=server to the input tag. (This will give you access to the file, its length, type, size, etc inside the handling method.)

Also, be aware that if you upload, say a 4MB file the aspnet_wp.exe process on your server will consume 4mb of memory; while not a big problem, the worker process will never fully relase the 4MB that it has consumed. So if you have, say 800 4MB uploads occur in a relatively short time that will consume well over a GB of memory! If your server only has 1024MB of memory, the process will recycle itself and the application will fail and a user that may be trying to upload a file at that time will get an error. Just an FYI.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 23rd, 2006, 09:25 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Thank you for your suggestions. I've checked the page where you've directed me, but think that, simple though it appears, this solution won't work in my instance. Primarily, the suggested solution appears to assume "fileupload" is pre-defined on the ASP form itself = i.e., that the line:

<input type=file id=fileUpload runat=server>

...is coded in the HTML of the ASP.NET page (i.e.-this code appears in formname.aspx). Unfortunately, this is not the case, and the input tag is actually being generated as needed by VB code using response.writes. So unfortunately, the code in the fileUpload function ...

if fileUpload.PostedFile.ContentLength = 0 then...

...produces an undefined reference in the VB code and refuses to compile because fileUpload is, indeed, undefined at compilation time.

While I could add "runat=server" easily enough to the generated code, the submit code which executes when the page is returned does not know that the fileUpload object may exist, in part because it is a different ASP.NET page from the one which created the page initially (action=another ASP.NET page), but also because the form may contain 0, 1, or many such controls, and not just one and only one. So, adding "runat=server" does not appear to resolve the compilation failure because it is directly trying to reference a control on an ASP.NET page that does not exist until run time.

Given that the file tag cannot be pre-defined as a server based control, it seems to me the only solution is to reference the file input tag indirectly, such as with request.form, but that's just my guess having never tried to do this before with out a net (.NET 2.0, that is). :D

So, how did classic ASP upload files prior to all the fancy .NET stuff that I unfortunately cannot seem to use in this instance?
 
Old October 23rd, 2006, 09:40 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

ASP used COM controls to upload files as it had no native support to upload files.

In so far as not being able add a control runtime, that is inaccurate. I do much the same thing as you do (though with drop down boxes) but its the same secenario there could be 0, 1, 2 etc drop down boxes on the page, however, I wired them all to the same method.

Of course you have to be able to think abstractly but it has to do with using a conatiner control (like a table or a panel) and using that controls controls.add(control) property to add the Server Control to the page. I then write all of this information out to the page and viola I have a dynamically created page with a minimal amount of effort.

hth

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 23rd, 2006, 09:45 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

I understand that I can add controls dynamically.

But how do you get the ASP.NET VB code that runs when the form is submitted to reference controls that do not exist until run time without throwing a compiler error?
 
Old October 23rd, 2006, 09:51 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Dont reference a control name.

In your method do something like this (i assume the container control is a panel)

For each ctrl as control in pnl1.controls
  If TypeOf ctrl Is HtmlInputFile then
   Dim fileUp as HtmlInputFile = Ctype(ctrl, HtmlInputFile)
   //do processing of file here
  End If
next


Now you can access the file and all of the control attributes like length, content type, etc

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 23rd, 2006, 10:30 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

OK, that elimininates the compilation error for the reference to the undefined input tag. But the "for each" code doesn't work - the file tag is not in a panel, but rather is buried in several layers of tables that may vary from run to run, but I don't think that's the problem in any case. Because when I try to search the entire form for HtmlIntputFile type fields using this code:

For each ctrl as control in me.controls

It says there's only one control on the page, the form tag, and it's the wrong form. "For each ctrl" appears to be looping through the controls on the page where the action was redirected when the generated form's submit button is being clicked, rather than the submitted form itself. IOW, when the user clicks the submit button on the generated form, they are not returned to the same .NET page as the one which generated the code, but to another one, and thats the form the "for each ctrl" loop appears to be looping though. So how do I get it to loop through the submitted form's controls to do this, rather than the action page's controls?

Alternatively, I know what the field names will be and maybe it's not really necessary to loop through every control testing the field type if I could get there directly using the field name, as with the "request.form" function. Is there another way to do this that does not require looping through fields on the form?
 
Old October 23rd, 2006, 10:42 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The Request.Form is a collection so you can loop through it but I have never used it in regards to what your asking.

You can do:

For each item in Request.Form
  //Item will contain the NAME of the element
 //to get the value you would do Request.Form(item)
Next

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature
 
Old October 23rd, 2006, 11:00 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Unfortunately...

For each ctrl as controls in form.request

throws this error when executed:

Specified cast is not valid.

And although "For each item in request.form" finds the expected field on the correct page, referencing "item" rather than "ctrl" in this line:

Dim fileUp As HtmlInputFile = CType(item, HtmlInputFile)

...throws the "invalid cast" error, too, so evidently "item" cannot be converted to an object of type HtmlInputFile the way "ctrl" can. If there's a way to use "item" to determine the "ctrl" value needed for the necessary conversion, this could work.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter File Types in ASP.NET File Upload ramuis78 ASP.NET 2.0 Basics 2 May 31st, 2007 10:50 AM
File Upload question - source directory turklet Beginning PHP 1 February 23rd, 2007 10:06 AM
File Upload Question Remains Unaswered Ron Howerton ASP.NET 1.0 and 1.1 Basics 23 October 25th, 2006 08:48 AM
File Upload Question Ron Howerton ASP.NET 2.0 Basics 3 October 23rd, 2006 01:06 PM
Whole Folder upload(Multi file Upload) ramasamy_rams XML 1 September 9th, 2005 12:43 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.