Dave Burke : Freelance .NET Web Developer specializing in Online Communities

HtmlInputFile posting from a usercontrol in OnUpdateCommand

As the subject says, I am posting a file in a Datalist <EditItemTemplate> in an OnUpdateCommand event method using a HtmlInputFile HTML Input control.  I've used HtmlInputFile quite a bit and its always worked great, but not today.

The usercontrol containing the HTMLInputFile control is loaded by another usercontrol, so perhaps that had something to do with it.  Everything was right, darn it.  The html <form enctype> was correct, the usual referencing worked for two textboxes in the form on OnUpdateCommand.

if (e.Item.ItemType == ListItemType.EditItem)
{
HtmlInputFile File1 = (HtmlInputFile) e.Item.FindControl("File1");
string attachment =File1.PostedFile.FileName.ToString();  <--Go straight to jail.  Do not pass go.
}

I was almost ready to pass the HtmlInputFile control's unique ID from the datalist's OnEditCommand to its OnUpdateCommand in Session and reference it that way (which probably would have worked.)

Then Carlos Medina comes in to save the day with an answer to another .PostedFile nerd in need. (Took me a long time to find this one!)

Carlos says....

one possible solutions maybe is working with the HttpRequest object directly.... for example...

public void DoItemUpdate(Object src, DataListCommandEventArgs e)
{
      string newfilename = "";
      if ( Request.Files.Count > 0 ) // if there are more than one upload file
      {
           HttpPostedFile file = Request.Files[0];
           // <snip>code to save uploaded file</snip>
           // file.SaveAs("");
      }
      //<snip>code to save data in DB</snip>
}
       

The check for the Request.Files.Count seemed to not be necessary, since a string.empty from the HtmlInputFile still created a request.files object.  If necessary, a (file.FileName != string.Empty) can be used for similar purposes.

HttpPostedFile file = Request.Files[0];
attachment = ProcessAttachment(file.FileName);
file.SaveAs(ConfigurationSettings.AppSettings["AttachmentDirectory"].ToString() + attachment);

So there it is.  A brand new way (to me) of processing a file uploaded from an HTMLInputFile control.  Most important, it fixed my error for posting from a seoncd-tier usercontrol!

Thank you Carlos, wherever you are.

Comments (0) | Post RSS RSS comment feed

Posted on 5/17/2004 12:41:00 PM by Dave Burke
Categories:
Tags:

Related posts


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke