Library code snippets

Uploading Files Using ASP.NET

Add a file browse HTML control. Right click and select 'Run as Server Control'. Add a button. So you have something like this:

<input type="file" runat="server" ID="File1" /><asp:Button runat="server" ID="Button1" /> 

Double click the button and add the following code.

private void Button1_Click(object sender, System.EventArgs e)
{
string strFilename;

try
{
strFilename = File1.PostedFile.FileName;
strFilename = System.IO.Path.GetFileName(strFilename);
File1.PostedFile.SaveAs(@"f:\"+strFilename);
}
catch(Exception ex)
{
Response.Write(ex);
}
}

Comments

  1. 25 Aug 2009 at 06:33

    Write permission must be given to upload folder for the asp.net account.

  2. 29 Sep 2006 at 20:09

    Have you solved this problem?  You might need to change the file right access right property for the folder and/or for the file. 

    What exactly is at line 56?

  3. 05 Jul 2006 at 07:41

    System.UnauthorizedAccessException: Access to the path "d:\build_info" is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at WebApplication1.WebForm1.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication1\webform1.aspx.cs:line 56

     

    what is this problem means??

  4. 04 May 2006 at 07:14

    In Web.config change the value of maxRequestLength ...

    Example (for 100mb):

      <httpRuntime maxRequestLength="100000" />

  5. 12 Apr 2006 at 11:55

    but this code cannot upload huge file size and this is a big problem for me

     

  6. 06 Apr 2006 at 09:07

    hi,
    Nice piece of code..
    I have a requirement where i need to upload a file to another system in the LAN.
    I have an ftp appication which does this for me. But i need to do it from ASP.NET itself..
    Is there a way to enhance the above code to transfer files across systems
    or can i call my ftp application from ASP.NET ?
    My ftp application is written in VB.

    Thanks in Anticipation.

    kris.











  7. 01 Jan 1999 at 00:00

    This thread is for discussions of Uploading Files Using ASP.NET.

Leave a comment

Sign in or Join us (it's free).

Man from Mars
AddThis

Related podcasts

Want to stay in touch with what's going on? Follow us on twitter!