Geeks With Blogs
Points To Share Mostly SharePoint

Overcoming the Asp.Net FileUpload limitations.  

In asp.net 2.0 and up, Microsoft introduced the FileUpload control. As you’ll see in the code below, usage is simple. Park it on the form in design mode, and use the FileUpload.SaveAs(URI) method to save it in a URI of your choice. Add a button, name it Upload (btnUpload) and that’s it. Now write the code as I have and try it.

  <% @ Page Language ="C#" AutoEventWireup ="true" CodeBehind ="Default.aspx.cs" Inherits ="FileUpload._Default" %>

  <! 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 > Untitled Page </ title >
</ head >
< body >
    < form id ="form1" runat ="server">

    < asp : FileUpload ID ="FileUpload1" runat ="server" />
    < p >
        < asp : Button ID ="btnUpload" runat ="server" onclick =" btnUpload_Click" Text ="Upload" />
    </ p >
    </ form >
</ body >
</ html >  

namespace FileUpload
{
    public partial class _Default : System.Web.UI. Page
    {
        protected void Page_Load( object sender, EventArgs e)
        {  

        }  
        protected void Button1_Click( object sender, EventArgs e)
        {
         Response.Write( "Blah" );
            try
            {
                FileUpload1.SaveAs( @"C:\temp\" + FileUpload1.FileName);
            }
            catch ( Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
}

 

So where are the controls shortcomings (which I mercifully named “limitations” in the title)? I immediately stumbled upon 2:  

1: You are limited to a 4 Meg upload   

This is something that comes disguised. When you try – this is what you get.  

The page cannot be displayed  

Even worse – the error is untrappable. If you notice the two bold “Response.Write” lines in the code – they don’t work. The code does not go there at all. I am told that this is a planned behavior designed to prevent the loading of large files as a denial of access attacks.   

I use the program as a replacement for a file upload in SharePoint, which means that all my uploads are within the intranet, so denial of access is not really my concern. Yes, there is a way to increase the size of the upload, but it involves changing the Machine Config. Not a thing that you do lightly, and for most of us, not a thing that we mere coders can do at all. Even if we could, we’ll always reach new limits, so we are better off thinking of another solution.  

2: you do not get the full path to the file, just the file name  

When I tried to get the full path so that I can use File.Copy instead, realized that I don’t get this comfort either.   

I had to come up with a different solution:   

Simply use a standard HTML control Input type=”file” insread, as you can see in the code below. The identical form except for this change, and the code behind utilizing the File.Copy method.  

<% @ Page Language ="C#" AutoEventWireup ="true" CodeBehind ="Default.aspx.cs" Inherits ="UploadVideo._Default" %>  

<! 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 > Untitled Page </ title >
</ head >
< body >
    < form id ="form1" runat ="server">
    < div >
    < p >
        < INPUT TYPE ="file" NAME ="FilePath" />< br />< br />
    < p >
        < asp : Button ID ="btnUpload" runat ="server" Text ="Upload" 
            onclick ="btnUpload_Click" />
    </ p >
    </ div >
    </ form >
</ body >
</ html >

namespace UploadVideo
{
    public partial class _Default : System.Web.UI. Page
    {
        protected void Page_Load( object sender, EventArgs e)
        {

        }  

        protected void btnUpload_Click( object sender, EventArgs e)
        {

            string inFilePath = Request.Params.Get( "filePath" );
            string outFileName = Path .GetFileName(inFilePath);
             string outFilePath = "C:\\Temp\" + outFileName;
            File .Copy(inFilePath, outFilePath);
        }
    }
}

 

That’s all Folks!!!!

 

Posted on Tuesday, December 6, 2011 10:09 AM | Back to top


Comments on this post: Asp.net File Upload limitations

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © PointsToShare | Powered by: GeeksWithBlogs.net