开发者

AsyncFileUpload restrict file size to upload

I am using AsyncFileUpload in order to allow users to asynchronously upload files.

I want to limit the size of the file to 1MB.

So far as to what I've seen I can only get the length of the file upon upload completion

like when upload starts:

(OnClientUploadStarted)

function UploadStarted(sender,args) 
{
   //if bigger than 1MB (approximately)
   if (args.get_length() > 1000000 ) 
   {
       ShowActionNotificationError( errorMessage); 
       return false;  
    }
}

args.get_length() is null , so I can't use it...

And when upload is completed:

(OnClientUploadComplete)

fun开发者_StackOverflow中文版ction UploadComplete(sender,args) 
{
    //if bigger than 1MB (approximately)
    if (args.get_length() > 1000000 ) 
    {
        ShowActionNotificationError( errorMessage); 
        return false;  
    }
}

works ok, but file was already uploaded...

So How can I know the size of the file before uploading it? am I missing something very simple?

I would really like to do it without handling HTTP Request length and the sort.

Thanks!


You can do this in the client side Upload Start event.

if (sender._inputFile.files[0].size >= maxFileSize) {
    sender._stopLoad();
}

_stopLoad will call your Upload Error event.


After some substantial research I realize this cannot be done using AJAX.

As I look around at sites like yahoo mail and gmail, it's done by Flash.

in hotmail it's done with Silverlight.

there is a free (for now) flash upload control called Uploadify...

I am now working on integrating it.

if I'm wrong please correct me! :)


You can check its size in FileUploadComplete method on server side

void FileUploadComplete(object objSender, AsyncFileUploadEventArgs e)
{
  if (e.get_length() > 1000000 )
  {  
      ShowActionNotificationError( errorMessage);
      return false;
  }
}


Yes what Mithir told is correct. Its really cumbersome to give the modern user experience using HTML control. Another alternative is SWFUpload


If you need to check the file size before the content is actually uploaded to the server, then your better off using asp component FileUpload. With this you can check and decide if you want to upload the file or not.

If FileUpload1.FileContent.Length <= DesiredFileSize Then

   <<Code to Upload the file>>

End If


CERO Feb 14 '12 at 5:51

The FileUpload1 no is functional for UpdatePanel:

Controls that are not compatible with UpdatePanel controls

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:

Treeview control under several conditions. One is when callbacks are enabled that are not part of an asynchronous postback. Another is when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. Another is when the EnableClientScript property is false (the default is true). Another is if you change the value of the EnableClientScript property between asynchronous postbacks.

Menu control when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles.

FileUpload and HtmlInputFile controls when they are used to upload files as part of an asynchronous postback.

GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.

Login, PasswordRecovery, ChangePassword, and CreateUserWizard controls whose contents have not been converted to editable templates.

The Substitution control.

To use a FileUpload or HtmlInputFile control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel. The FileUpload and HtmlInputFile control can be used only in postback scenarios.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜