Actions
Share

How to setup file uploads and change the maximum upload size allowed

Here is a basic walk through of how to setup file uploads on the MaxESP platform. We'll also show you how to change the default (4 MB) set at the server level for the maximum file size you want to allow your users to upload into your target folder.

On your MaxESP site you'll need to perform the following steps:
  1. Add or confirm the following entries in the root web.config file
    1. Set a machineKey (use our tools / walkthrough if you need help)
    2. WARNING: If you receive exceptions after setup you may want to turn off customErrors (system.web/customErrors), and httpErrors (system.webServer/httpErrors) so you can see the real exception being thrown. Turn them back on after you've fixed your problems.
  2. Create a new folder named Uploads
  3. In our Control Panel drill into the management tools for this site, and use the Folder Permissions tool to mark the new folder Writable so the user has the appropriate permissions to create files and folders in your site
    1. Security Recommendation: Use ASP.NET membership and URL Authorization to control which users can access your File upload section and support pages
  4. Create the file upload page. You can use the code below, but make sure you point the path correctly at the location of the Uploads folder created in step 2.
How to change the maximum upload size
By default ASP.NET enforces a 4 MB maximum request length. To change this you can add this to your root web.config in the <system.web/> section.

    <httpRuntime maxRequestLength="5120"/>

This will now allow your users to upload up to 5 MB (5 * 1024 kb) files.

WARNING:
You may need to change the timeout values for your page and scripts so the default timeout doesn't occur before the file is completely uploaded into the target.

Additional Information
Here is supporting documentation from Microsoft's MSDN resource.

http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxrequestlength.aspx

FileUpload.aspx

<%@ Page Language="C#"%> 
<script runat="server"
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!this.IsPostBack) 
            uploadTarget.Text = this.MapPath("Uploads"); 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        if (FileUpload1.HasFile) 
           try  
           { 
               string path = System.IO.Path.Combine(uploadTarget.Text, FileUpload1.FileName); 
               FileUpload1.SaveAs(path); 
            
               Label1.Text = "File name: " + 
                   FileUpload1.PostedFile.FileName + "<br>" + 
                   FileUpload1.PostedFile.ContentLength + " kb<br>" + 
                   "Content type: " + 
                   FileUpload1.PostedFile.ContentType;   
           } 
            catch (Exception ex) { 
              Label1.Text = "ERROR: " + ex.Message.ToString();  
            } 
        else 
        { 
           Label1.Text = "You have not specified a file."
        } 
    } 
</script> 
 
<!DOCTYPE html  
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>File Upload</title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <table> 
            <tr><td><asp:TextBox Width="500" runat="server" ID="uploadTarget" /></td></tr> 
            <tr><td><asp:FileUpload ID="FileUpload1" runat="server" /></td></tr> 
            <tr><td><asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" /></td></tr> 
            <tr><td><asp:Label ID="Label1" runat="server"></asp:Label></td></tr> 
        </table> 
    </div> 
    </form> 
</body> 
</html> 
 

Article ID: 747, Created On: 2/5/2010, Modified: 2/8/2010

Feedback (0)