Uploading files to Web server

Discussion in 'ASP .Net' started by FusionGuy, Oct 17, 2005.

  1. FusionGuy

    FusionGuy Guest

    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.
     
    FusionGuy, Oct 17, 2005
    #1
    1. Advertising

  2. 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.
    >
    >
    >
     
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN, Oct 17, 2005
    #2
    1. Advertising

  3. FusionGuy

    FusionGuy Guest

    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" <> wrote
    in message news:...
    > 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.
    >>
    >>
    >>
     
    FusionGuy, Oct 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason

    Uploading files to the web server

    Jason, Jan 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    352
    Jason
    Jan 20, 2004
  2. FusionGuy

    Problem uploading files to Web server

    FusionGuy, Oct 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    FusionGuy
    Oct 12, 2005
  3. Dung Ping
    Replies:
    10
    Views:
    995
    Chaddy2222
    Jan 16, 2006
  4. Phil Johnson
    Replies:
    3
    Views:
    952
    Phil Johnson
    Apr 9, 2008
  5. wym
    Replies:
    0
    Views:
    224
Loading...

Share This Page