Friday, July 23, 2010

How to upload a file –

When uploading a file there are two main concerns.
  1. Do we have correct permissions to the directory? work process should have write permissions to the upload directory in-order to save the uploaded file.
  2. The file size that we try to upload, does it exceed the allowed request limit of by the .net runtime? This example allows requests smaller than 2 MB (2024 KB)
  <httpRuntime maxRequestLength="2024" />
In this case provide a suitable error message for error 404.13
Here is a complete example to demonstrate how to upload a file using
<%@ Page Language="C#" %>
<html xmlns="">
    <head id="Head2" runat="server">
        <script runat="server">
            string uploadPath = "Uploads";
            public void Save(object sender, EventArgs e)
                if (this.fuFile.HasFile)
                    string filepath = Server.MapPath(Path.Combine(this.uploadPath, this.fuFile.FileName));
                    if (!File.Exists(filepath))
                        this.lblResults.Text = "File uploaded successfully";
                        this.lblResults.Text = "File already exists";
                    this.lblResults.Text = "Please slect a file";
        <form id="form2" runat="server">
            <asp:FileUpload runat="server" ID="fuFile" />
            <asp:Button runat="server" ID="btnSave" OnClick="Save" Text="Save" />
            <asp:Label runat="server" ID="lblResults" />


Unknown said...

I hope the file extension will be .aspx. Am I correct?

Charith Shyam Gunasekara said...

Yes it is... but this does not use a HttpHandler

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...