Uploading files to Web server

F

FusionGuy

I've created a file uploading handler, implemented as an httpHandler. Each
time I attempt to upload a file, or files, my HttpContext.Request.Files
property never contains the files that were uploaded. Here's a snippet of
my handler code:

// *** BEGIN HANDLER CODE *** //
public class AutoUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
HttpFileCollection coll = context.Request.Files;

if (request.Files.Count > 0)
{
// code to save files locally...
}
}
}
// *** END HANDLER CODE *** //

I'm posting files using the normal "<input type="file">" method and also
ensuring that my enctype is set to "multipart/form-data" Just to be as
verbose as possible, here's a snippet of the client-side code:

// *** BEGIN CLIENT CODE *** //
<form id="Form1" method="post" enctype="multipart/form-data"
action="http://localhost/UploaderService/Uploader.aspx">
<input type="file" id="txtUpload" style="WIDTH:250px">
<br>
<br>
<input type="submit" value="Post It">
</form>
// *** END CLENTCODE *** //

I've checked to make sure that I have Write permissions to the target
directory and also have Write permissions set to allowed in IIS. Is there
something I've missed here?? Thanks in advance.
 
G

Guest

The norm is to have the file input 'runat="server"'. You can then get its
information and upload. Sample scripts:

ASPX page

<INPUT id="filUpload" type="file" name="filUpload" runat="server">

CodeBehind on upload button click event

//C#
if (filUpload.PostedFile != null)
{
filUpload.PostedFile.SaveAs(locationOnWebServer + fileName);
}

'VB.NET
If Not filUpload.PostedFile Is Nothing Then
filUpload.PostedFile.SaveAs(locationOnWebServer + fileName);
End If

Or, if you want more control:

//C#
if (filUpload.PostedFile != null)
{
//Get the file
HttpPostedFile file = fileUpload.PostedFile;
Int32 fileLength = file.ContentLength;
string fileName = file.FileName;
byte[] buffer = new byte[fileLength];
file.InputStream.Read(buffer, 0, fileLength);

//Save the file (can be placed in another routine)
FileStream newFile = new FileStream(path, FileMode.Create);
newFile.Write(buffer, 0, buffer.Length);
newFile.Close();
}

'VB.NET
If Not filUpload.PostedFile Is Nothing Then
Dim file As HttpPostedFile = filUpload.PostedFile

'Find its attributes
Dim fileLength As Int32 = file.ContentLength
Dim fileName As String = file.FileName

'Now let's try to save this file
Dim buffer(fileLength) As Byte
file.InputStream.Read(buffer, 0, fileLength)

Dim newFile As New FileStream(path, FileMode.Create)
newFile.Write(buffer, 0, buffer.Length)
newFile.Close()
End If


Examples of each
http://www.aspheute.com/english/20000802.asp
http://www.codeproject.com/aspnet/fileupload.asp

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
F

FusionGuy

Hi Cowboy,

Thanks for your post, but unfortunately I need to create a more generic file
upload service than that, hence why my code doesn't use server-side web
controls.


Cowboy (Gregory A. Beamer) - MVP said:
The norm is to have the file input 'runat="server"'. You can then get its
information and upload. Sample scripts:

ASPX page

<INPUT id="filUpload" type="file" name="filUpload" runat="server">

CodeBehind on upload button click event

//C#
if (filUpload.PostedFile != null)
{
filUpload.PostedFile.SaveAs(locationOnWebServer + fileName);
}

'VB.NET
If Not filUpload.PostedFile Is Nothing Then
filUpload.PostedFile.SaveAs(locationOnWebServer + fileName);
End If

Or, if you want more control:

//C#
if (filUpload.PostedFile != null)
{
//Get the file
HttpPostedFile file = fileUpload.PostedFile;
Int32 fileLength = file.ContentLength;
string fileName = file.FileName;
byte[] buffer = new byte[fileLength];
file.InputStream.Read(buffer, 0, fileLength);

//Save the file (can be placed in another routine)
FileStream newFile = new FileStream(path, FileMode.Create);
newFile.Write(buffer, 0, buffer.Length);
newFile.Close();
}

'VB.NET
If Not filUpload.PostedFile Is Nothing Then
Dim file As HttpPostedFile = filUpload.PostedFile

'Find its attributes
Dim fileLength As Int32 = file.ContentLength
Dim fileName As String = file.FileName

'Now let's try to save this file
Dim buffer(fileLength) As Byte
file.InputStream.Read(buffer, 0, fileLength)

Dim newFile As New FileStream(path, FileMode.Create)
newFile.Write(buffer, 0, buffer.Length)
newFile.Close()
End If


Examples of each
http://www.aspheute.com/english/20000802.asp
http://www.codeproject.com/aspnet/fileupload.asp

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************


FusionGuy said:
I've created a file uploading handler, implemented as an httpHandler.
Each
time I attempt to upload a file, or files, my HttpContext.Request.Files
property never contains the files that were uploaded. Here's a snippet
of
my handler code:

// *** BEGIN HANDLER CODE *** //
public class AutoUpload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest request = context.Request;
HttpResponse response = context.Response;
HttpFileCollection coll = context.Request.Files;

if (request.Files.Count > 0)
{
// code to save files locally...
}
}
}
// *** END HANDLER CODE *** //

I'm posting files using the normal "<input type="file">" method and also
ensuring that my enctype is set to "multipart/form-data" Just to be as
verbose as possible, here's a snippet of the client-side code:

// *** BEGIN CLIENT CODE *** //
<form id="Form1" method="post" enctype="multipart/form-data"
action="http://localhost/UploaderService/Uploader.aspx">
<input type="file" id="txtUpload" style="WIDTH:250px">
<br>
<br>
<input type="submit" value="Post It">
</form>
// *** END CLENTCODE *** //

I've checked to make sure that I have Write permissions to the target
directory and also have Write permissions set to allowed in IIS. Is
there
something I've missed here?? Thanks in advance.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top