Uploading files to Web server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FusionGuy

    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.Req uest.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.Respons e;
    HttpFileCollect ion 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="multip art/form-data"
    action="http://localhost/UploaderService/Uploader.aspx">
    <input type="file" id="txtUpload" style="WIDTH:25 0px">
    <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.


  • Cowboy (Gregory A. Beamer) - MVP

    #2
    RE: Uploading files to Web server

    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.Post edFile != null)
    {
    filUpload.Poste dFile.SaveAs(lo cationOnWebServ er + fileName);
    }

    'VB.NET
    If Not filUpload.Poste dFile Is Nothing Then
    filUpload.Poste dFile.SaveAs(lo cationOnWebServ er + fileName);
    End If

    Or, if you want more control:

    //C#
    if (filUpload.Post edFile != null)
    {
    //Get the file
    HttpPostedFile file = fileUpload.Post edFile;
    Int32 fileLength = file.ContentLen gth;
    string fileName = file.FileName;
    byte[] buffer = new byte[fileLength];
    file.InputStrea m.Read(buffer, 0, fileLength);

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

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

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

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

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


    Examples of each



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

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


    "FusionGuy" wrote:
    [color=blue]
    > I've created a file uploading handler, implemented as an httpHandler. Each
    > time I attempt to upload a file, or files, my HttpContext.Req uest.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.Respons e;
    > HttpFileCollect ion 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="multip art/form-data"
    > action="http://localhost/UploaderService/Uploader.aspx">
    > <input type="file" id="txtUpload" style="WIDTH:25 0px">
    > <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.
    >
    >
    >[/color]

    Comment

    • FusionGuy

      #3
      Re: Uploading files to Web server

      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" <NoSpamMgbworld @comcast.netNoS pamM> wrote
      in message news:C92CC458-D33A-43D5-96C3-82A49473B6DF@mi crosoft.com...[color=blue]
      > 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.Post edFile != null)
      > {
      > filUpload.Poste dFile.SaveAs(lo cationOnWebServ er + fileName);
      > }
      >
      > 'VB.NET
      > If Not filUpload.Poste dFile Is Nothing Then
      > filUpload.Poste dFile.SaveAs(lo cationOnWebServ er + fileName);
      > End If
      >
      > Or, if you want more control:
      >
      > //C#
      > if (filUpload.Post edFile != null)
      > {
      > //Get the file
      > HttpPostedFile file = fileUpload.Post edFile;
      > Int32 fileLength = file.ContentLen gth;
      > string fileName = file.FileName;
      > byte[] buffer = new byte[fileLength];
      > file.InputStrea m.Read(buffer, 0, fileLength);
      >
      > //Save the file (can be placed in another routine)
      > FileStream newFile = new FileStream(path , FileMode.Create );
      > newFile.Write(b uffer, 0, buffer.Length);
      > newFile.Close() ;
      > }
      >
      > 'VB.NET
      > If Not filUpload.Poste dFile Is Nothing Then
      > Dim file As HttpPostedFile = filUpload.Poste dFile
      >
      > 'Find its attributes
      > Dim fileLength As Int32 = file.ContentLen gth
      > Dim fileName As String = file.FileName
      >
      > 'Now let's try to save this file
      > Dim buffer(fileLeng th) As Byte
      > file.InputStrea m.Read(buffer, 0, fileLength)
      >
      > Dim newFile As New FileStream(path , FileMode.Create )
      > newFile.Write(b uffer, 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:
      >[color=green]
      >> I've created a file uploading handler, implemented as an httpHandler.
      >> Each
      >> time I attempt to upload a file, or files, my HttpContext.Req uest.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.Respons e;
      >> HttpFileCollect ion 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="multip art/form-data"
      >> action="http://localhost/UploaderService/Uploader.aspx">
      >> <input type="file" id="txtUpload" style="WIDTH:25 0px">
      >> <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.
      >>
      >>
      >>[/color][/color]


      Comment

      Working...