Showing posts with label AsyncFileUpload. Show all posts
Showing posts with label AsyncFileUpload. Show all posts

Monday, August 6, 2012

Unhandled Exception: "The file attached is invalid" with AsyncFileUploader and .NET Framework 4

 

I upgraded my ASP.NET framework from 3.5 to 4 recently, and encountered a host of problems.

I knew the problem was due to the upgrade because I had not changed my code, and yet, there were parts of the system that went haywire with seemingly esoteric errors.

Today, I got the error below when I tried to upload images on a web page that was working perfectly before.

2012-08-06_102203

 

Fortunately, the error is easy to solve.  I just followed the tip from ndkjava given on the ASP.NET forum.

Basically, what you need to do is to locate the control on the markup code and add this attribute to it.

ClientIDMode="AutoID"

For example, this is how my code looks like after adding it.

<asp:AsyncFileUpload ClientIDMode="AutoID" ID="AsyncFileUpload1" runat="server"  onuploadedcomplete="AsyncFileUpload1_UploadedComplete" />

Everything seems to work marvellously after that! :)


ps. If you want to download the latest version of the FileAsyncUpload which is in the ASP.NET AJAX Control Toolkit 4, you can get it at CodePlex here (updated 24 Jun 2012).


For tutorials on how to use the AJAX Control Toolkit FileAsyncUpload control,  you can find one here.

Friday, December 31, 2010

Showing the size of an image after it has been uploaded using AJAX AsyncFileUpload file control

On Wednesday, I was working on some code that will accept an upload of an image file and after the image upload is complete, the page will display the dimensions of the image that was uploaded.

I had problems getting my original code to work though. I getting this error "The process cannot access the file because it is being used by another process". Below is my original code.

AsyncFileUpload upload = (AsyncFileUpload)sender;
   String filename = System.IO.Path.GetFileName(upload.FileName);
   String filePathName = Server.MapPath("images/") + filename;
   upload.SaveAs(filePathName);

   System.Drawing.Image image = System.Drawing.Image.FromFile(filePathName);
  
   if (image != null){            
     String msg = "Uploaded " + filename + " of size " + 
                  image.Width + "x" + image.Height;
     Label1.Text = msg;
}

After numerous trials and errors and after alot of Googling, I finally figured out that the problem was due to a GDI+ bug that locks up the Image handle.

The thread above suggested some workarounds for the problem, but I didn't try them, instead, I thought since I was encountering the lock problem because one process was trying to save the File, while another process was trying to read the file, maybe I can try to read the file by creating an Image from Stream instead of disk instead.

Sure enough, after I managed to find out how to create an Image object from Stream and modified my code accordingly, the thing worked! ^_^

Anyway, the modified code only needed one change, that is to change the line of code as highlighted below:

protected void AsyncFileUpload1_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
    {
        AsyncFileUpload upload = (AsyncFileUpload)sender;
        String filename = System.IO.Path.GetFileName(upload.FileName);

        String filePathName = Server.MapPath("images/") + filename;
        upload.SaveAs(filePathName);


        System.Drawing.Image image = System.Drawing.Image.FromStream(upload.FileContent);

        if (image != null)
        {            
            String msg = "Uploaded " + filename 
                         + " of size " + image.Width + "x" + image.Height;
            Label1.Text = msg;
        }
    }

Tuesday, December 28, 2010

AJAX Control Toolkit AsyncFileUpload control

I was using AJAX Control Toolkit AsyncFileUpload control just now and hit an error "The file attached is invalid".

Fortunately, the error was easy to solve, just needed to add the following attribute
on the Async Upload Control

clientIdMode="AutoID"

Got the tip from the link below ^_^
ASP Net Forums