Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 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 20th, 2006, 03:22 PM
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 an ASP.NET application that generates HTML code. By this, I mean that the code uses write.response to write HTML code directly to the browser for the entire page:

Reponse.Write("<HTML lang=""en"" METHOD=""POST"" ENCTYPE=""multipart/form-data"">")

This is a problem, because I need to enable the generated form to upload files to a folder on the server. I'm not sure about this, but it seems unlikely to me that I can generate ASP.NET fileupload controls to do this because they are translated to input type="file" before delivery to the browser. If I'm wrong about that, please let me know, but for now I'm assuming that I must use the old way:

Response.Write("<input type=""file"" name=""filAttachment"" size=30> ")

This works OK to find the file to be uploaded and, in fact, that's pretty much what I see in the browser source code when asp:fileupload is translated.

But I seem to be missing the reference to whatever sits on the server to save the file when the form is submitted. .NET allows you to reference the upload control directly in VB and it's as simple as using the .SAVEAS method:

filAttachment.PostedFile.SaveAs...

But of course, I'd get a compiler error trying to reference a field that's defined in generated HTML but not on the .NET form itself because VS doesn't know it exists without the form definition. I could put an asp:fileupload object on the .NET page before adding my generated HTML code, but then I'd be limited to however many such fields I put there, and users always want more. So, I'd rather not go that route if I can help it.

Has anybody got an example of the server side code needed to save uploaded files or an alternative suggestion on how to accomplish this?
 
Old October 21st, 2006, 03:30 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ron,

The problem with your method (e.g. outputting HTML for the upload controls) is that they end up as simple HTML client controls, with no server side counterpart.

What you could do is create true server side Upload controls and add them to the page.

Something like this:
Code:
Dim myUpload As FileUpload = New FileUpload()
myUpload.ID = "FileUpload1"
myPlaceholder.Controls.Add(myUpload)

myUpload = New FileUpload()
myUpload.ID = "FileUpload2"
myPlaceholder.Controls.Add(myUpload)

myUpload = New FileUpload()
myUpload.ID = "FileUpload3"
myPlaceholder.Controls.Add(myUpload)
This adds three FileUpload controls to a PlaceHolder control.

Then at post back, you can do something like this:
Code:
Dim myUpload As FileUpload = CType(Page.FindControl("FileUpload1"), FileUpload)
' Do something with the first file here.
myUpload = CType(Page.FindControl("FileUpload2"), FileUpload)
' Do something with the second file here etc
You should create the controls in the Page_Init event handler so they are available and filled by the time you need them.

For more info:

http://support.microsoft.com/default.aspx/kb/317515

or search Google for create controls dynamically.

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old October 23rd, 2006, 08:42 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

Hi Imar,

Thanks for your response. I've unfortunately just discovered I posted my question in the wrong forum. The application runs in 1.1, not 2.0, and your suggested solution doesn't appear to work there. At least, I can't seem to reference the FileUpload object there, anyway.

I've reposted my question to the 1.1 beginners forum instead. Sorry for the confusion.
 
Old October 23rd, 2006, 01:06 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ron,

In ASP.NET 2, the new WebControl FileUpload was introduced. For .NET 1.x, you can use the HtmlControl HtmlInputFile. So something like this should work (copied from a working application):
Code:
private void CreateUploadControls()
{
  maxImageControls = 5;  
  for (int i = 0; i < maxImageControls; i++)
  {
    HtmlInputFile myControl = new HtmlInputFile();
    myControl.ID = "FileUpload" + i.ToString();
    myControl.Style.Add("width" , "300px");
    myControl.Attributes.Add("class", "MyClass");
    plcUploads.Controls.Add(myControl);
    plcUploads.Controls.Add(new LiteralControl("<br /><br />"));
  }
}
This code adds five upload controls; each separated by two line breaks.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)




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 1.0 and 1.1 Basics 17 October 23rd, 2006 01:08 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.