Silverlight Multi-file breakpoint extension implementation code

Source: Internet
Author: User
Tags file size file upload silverlight

In the use of the dependent properties of the state change to update some data, such as the uploaded file size, and file upload status, and so on, due to a lot of code, pick some similar code below, the source code published later.

The code is as follows Copy Code

<summary>
Number of bytes currently uploaded (this is different from the same name in FileCollection, FileCollection is the total number of bytes of all files uploaded)
</summary>
Public double bytesuploaded
{
get {return _bytesuploaded;}
Set
{
_bytesuploaded = value;

Notifypropertychanged ("bytesuploaded");

Percentage = (int) ((value *)/_filestream.length);

}
}
#region INotifyPropertyChanged Members

private void Notifypropertychanged (string prop)
{
if (propertychanged!= null)
{
PropertyChanged (This, new PropertyChangedEventArgs (prop));
}
}

public event PropertyChangedEventHandler PropertyChanged;

There is also the upload of files using partitioned processing, select a byte[] buffer = new BYTE[4 * 4096] size of memory, the file into n blocks so large file, in the loop upload to the final completion.

This is the most important piece of code in FileUploader.cs, as shown below,

The code is as follows Copy Code

///<summary>
       ///upload file
        ///</summary>
        private void uploadadvanced ()
        {
            
            byte[] buffer = new byte[4 * 4096];
& nbsp;           int bytesread =  _file. FileStream.Read (buffer, 0, buffer. Length);          

Is the file uploaded?
if (bytesread!= 0)
{
_datasent + = Bytesread;

if (_datasent = = _datalength)
_lastchunk = true;//is the last piece of data, so WCF will decide whether to rename the temporary file on the server based on that information

               //uploading current data blocks
                _client. Storefileadvancedasync (_file. FileName, buffer, Bytesread, _initparams, _firstchunk, _lastchunk);
              
                //After the first message is False
                 _firstchunk = false;

               //Notification upload Progress Modification
                onprogresschanged ();
           }
            Else
             {
                //When finished uploading
                 _file. Filestream.dispose ();
                _file. Filestream.close ();

_client. Channelfactory.close ();
}
}

Then in the process of uploading need to always determine whether the file is deleted, completed or interrupted, in the FileUploader.cs need to pay attention to this code,

The code is as follows Copy Code

void _client_storefileadvancedcompleted (object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Check the Web service for errors
if (e.error!= null)
{
Discard the upload when the error occurs
_file. state = Constants.FileStates.Error;
}
Else
{
If the file is not canceled and not uploaded, continue with the upload
if (!_file. IsDeleted &&!_file. Isstop)
Uploadadvanced ();
}
}

The need to limit file size and the number of files uploaded at the same time requires attention to mpost. The initparameters parameter in the silverlightmultifileuploadtestpage.aspx, which can be configured in the configuration file, can change the code in the Code Page.xaml.cs if it is to be changed to a configuration file.

The code is as follows Copy Code

<summary>
Load configuration parameters then from. Config file
</summary>
<param name= "InitParams" ></param>
private void Loadconfiguration (idictionary<string, string> initparams)
{
String trytest = String. Empty;

Load Custom configuration information string
if (Initparams.containskey ("Customparam") &&!string. IsNullOrEmpty (initparams["Customparam"))
_customparams = initparams["Customparam"];

            if (Initparams.containskey ("Maxuploads") &&!string. IsNullOrEmpty (initparams["Maxuploads"]))
            {
                int. TryParse (initparams["Maxuploads"], out _maxupload);            
           }

if (Initparams.containskey ("maxfilesizekb") &&!string. IsNullOrEmpty (initparams["maxfilesizekb"))
{
if (int. TryParse (initparams["maxfilesizekb"], out _maxfilesize))
_maxfilesize = _maxfilesize * 1024;
}

if (Initparams.containskey ("FileFilter") &&!string. IsNullOrEmpty (initparams["FileFilter"))
_filefilter = initparams["FileFilter"];


           //Get relevant information from the configuration file
             if (!string. IsNullOrEmpty (configurationmanager.appsettings["maxfilesizekb"]))
             {
                 if (int. TryParse (configurationmanager.appsettings["maxfilesizekb"], out _maxfilesize))
                     _maxfilesize = _maxfilesize * 1024;
           }


if (!string. IsNullOrEmpty (configurationmanager.appsettings["Maxuploads"))
Int. TryParse (configurationmanager.appsettings["Maxuploads"], out _maxupload);

if (!string. IsNullOrEmpty (configurationmanager.appsettings["FileFilter"))
_filefilter = configurationmanager.appsettings["FileFilter"];

}


There are other readers can see their own code debugging run test, the following is the operation of the screenshot, the environment is VS2010, silverlight4.0 environment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.