Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Uploading files to Web server

Reply
Thread Tools

Uploading files to Web server

 
 
FusionGuy
Guest
Posts: n/a
 
      10-17-2005
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.


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBNVlA=?=
Guest
Posts: n/a
 
      10-17-2005
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.
>
>
>

 
Reply With Quote
 
 
 
 
FusionGuy
Guest
Posts: n/a
 
      10-17-2005
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" <(E-Mail Removed)> wrote
in message news:(E-Mail Removed)...
> 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.
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection to web server lost uploading files of 100mb Phil Johnson ASP .Net 3 04-09-2008 08:57 AM
ftp uploading vs. control panel uploading Dung Ping HTML 10 01-16-2006 10:43 AM
Problem uploading files to Web server FusionGuy ASP .Net 0 10-12-2005 06:33 PM
Web Service: Uploading files and setting file security on a file server wym ASP .Net Web Services 0 04-26-2004 02:02 PM
Uploading files to the web server Jason ASP .Net 2 01-20-2004 02:28 PM



Advertisments