473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Uploading files to Web server

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.
Nov 19 '05 #1
2 3917
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" wrote:
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.

Nov 19 '05 #2
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" <No************@comcast.netNoSpamM> wrote
in message news:C9**********************************@microsof t.com...
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" wrote:
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.

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Chamomile | last post by:
can anyone point me to some straightforward information on file uploading without using an html form? That is, direcly from within a php script. if I know the local path etc. to a particular...
5
by: Kevin Ollivier | last post by:
Hi all, I've come across a problem that has me stumped, and I thought I'd send a message to the gurus to see if this makes sense to anyone else. =) Basically, I'm trying to upload a series of...
2
by: rbt | last post by:
Has anyone used pure python to upload files to a webdav server over SSL? I have no control over the server. I can access it with all of the various webdav GUIs such as Konqueror, Cadaver, etc. by...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
3
by: Dean Richardson | last post by:
Hi, I'm having trouble uploading files via a PHP script. Whenever I upload a file greater than 10K, the file gets corrupted. However, text files upload OK. When I check the FTP Server log I...
2
by: Kausar | last post by:
Hello All, I want to know how to scan the files before uploading it to the server in ASP.NET 2.0 application. One work arround i thought is to allow to upload in some temporary folder then sacn...
7
by: Lad | last post by:
If a user will upload large files via FTP protocol, must the user have an FTP client on his computer or is it possible to use a similar way to "http form" comunication? Or is there another way(...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
1
by: =?Utf-8?B?RGFu?= | last post by:
MS won't seem to let me reply to my old post, so I created a new one. The error occurs in all browsers. It's definitely a server issue, not client. The server is not proxied in any way. I tried...
0
by: LoriFranklin | last post by:
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form. I am using some code that I found from Jacob at...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.