Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: Uploading a file in asp.net


Message #1 by "Jonas Bush" <jdbush4@c...> on Tue, 23 Jul 2002 21:01:30
I'm trying to upload files with the HtmlInputFile control, 
and I keep getting an "Object reference not set to an 
instance of an object." error, when calling 
UserFile.PostedFile.SaveAs()...code is as follows:


<INPUT id="UserFile" type="file" runat="server">

(and later...)

<input class="InputButton" id="btnUpload2" type="submit" 
value="Upload" runat="server">

In my codebehind, I have the following:

this.btnUpload2.ServerClick += new System.EventHandler
(this.btnUpload2_ServerClick);

private void btnUpload2_ServerClick(object sender, 
System.EventArgs e)
{
if(!ValidatePage())
	return;
string UploadPath = strServerPath + @"\Uploads\" + 
UserID.ToString() + @"\";
if(!Directory.Exists(UploadPath))
{
	Directory.CreateDirectory(UploadPath);
}
string strClPath = UserFile.Value;			
string strFileName = strClPath.Substring
((strClPath.LastIndexOf(@"\")+1));
UploadPath += strFileName;			
UserFile.PostedFile.SaveAs(UploadPath);		
}

Anyone have any suggestions?  Thanks...

Jonas

Message #2 by "Jonas Bush" <jdbush4@c...> on Wed, 24 Jul 2002 19:05:31
Figured it out, needed the correct enctype in the <FORM> tag. :D


> I'm trying to upload files with the HtmlInputFile control, 
a> nd I keep getting an "Object reference not set to an 
i> nstance of an object." error, when calling 
U> serFile.PostedFile.SaveAs()...code is as follows:

> 
<> INPUT id="UserFile" type="file" runat="server">

> (and later...)

> <input class="InputButton" id="btnUpload2" type="submit" 
v> alue="Upload" runat="server">

> In my codebehind, I have the following:

> this.btnUpload2.ServerClick += new System.EventHandler
(> this.btnUpload2_ServerClick);

> private void btnUpload2_ServerClick(object sender, 
S> ystem.EventArgs e)
{> 
i> f(!ValidatePage())
	> return;
s> tring UploadPath = strServerPath + @"\Uploads\" + 
U> serID.ToString() + @"\";
i> f(!Directory.Exists(UploadPath))
{> 
	> Directory.CreateDirectory(UploadPath);
}> 
s> tring strClPath = UserFile.Value;			
s> tring strFileName = strClPath.Substring
(> (strClPath.LastIndexOf(@"\")+1));
U> ploadPath += strFileName;			
U> serFile.PostedFile.SaveAs(UploadPath);		
}> 

> Anyone have any suggestions?  Thanks...

> Jonas


  Return to Index