Recently, when I was working on a Silverlight project, I often encountered file uploads. When I saw resumable data transfer in my 163 mailbox, I felt that the user experience was really good, it took some time to study the resumable upload function of Silverlight.
I found this feature in my blog. Later I saw a blog by Dai zhenjun, where discuznt used Silverlight to upload multiple files and could upload multiple files, however, if resumable upload is not implemented, you can modify the code by yourself to implement the Resume function after a Simple pause, if you want to resume data transfer after the next login as in the 163 mailbox, you need to save the paused status. Thank you for your technical support and selfless code dedication.
In this example, the status of the dependency attribute changes are used to update some data from time to time, such as the size of the uploaded file and the status of the uploaded file. Because there are many codes, select some similar code as follows, and publish the source code later.
/// <Summary>
/// The number of uploaded bytes (this is different from the attribute with the same name in filecollection, and the total number of bytes of all uploaded files in filecollection)
/// </Summary>
Public double bytesuploaded
{
Get {return _ bytesuploaded ;}
Set
{
_ Bytesuploaded = value;
Notifypropertychanged ("bytesuploaded ");
Percentage = (INT) (value * 100)/_ filestream. Length );
}
}
# Region inotifypropertychanged members
Private void policypropertychanged (string prop)
{
If (propertychanged! = NULL)
{
Propertychanged (this, new propertychangedeventargs (PROP ));
}
}
Public event propertychangedeventhandler propertychanged;
When uploading files, you can also use multipart processing. Select a byte [] buffer = new byte [4*4096] memory to split the files into N files of this size, after the upload is completed in a loop.
This code is the most important in fileuploader. CS, as shown below,
/// <Summary>
/// Upload a file
/// </Summary>
Private void uploadadvanced ()
{
Byte [] buffer = new byte [4*4096];
Int bytesread = _ file. filestream. Read (buffer, 0, buffer. Length );
// Is the File Uploaded?
If (bytesread! = 0)
{
_ Datasent + = bytesread;
If (_ datasent = _ datalength)
_ Lastchunk = true; // whether it is the last piece of data. Therefore, the WCF server determines whether to rename the temporary file based on the information.
// Upload the current data block
_ Client. storefileadvancedasync (_ file. filename, buffer, bytesread, _ initparams, _ firstchunk, _ lastchunk );
// Always false after the first message
_ Firstchunk = false;
// Notify the upload progress Modification
Onprogresschanged ();
}
Else
{
// After the upload is complete
_ File. filestream. Dispose ();
_ File. filestream. Close ();
_ Client. channelfactory. Close ();
}
}
In addition, during the upload process, you must always determine whether the file is deleted, completed, or interrupted. In fileuploader. CS, you need to pay attention to this code,
Void _ client_storefileadvancedcompleted (Object sender, system. componentmodel. asynccompletedeventargs E)
{
// Check whether the Web Service has an error
If (E. Error! = NULL)
{
// Abort upload when an error occurs
_ File. State = constants. filestates. error;
}
Else
{
// If the file is not canceled and does not exist, continue to upload
If (! _ File. isdeleted &&! _ File. isstop)
Uploadadvanced ();
}
}
The file size must be limited, and the number of files simultaneously uploaded must be concerned with mpost. silverlightmultifileuploadtestpage. the initparameters parameter in aspx can also be configured in the configuration file. If you want to change it to a configuration file, you can change the code page. XAML. the code in CS.
/// <Summary>
/// Load the configuration parameter then from. config file
/// </Summary>
/// <Param name = "initparams"> </param>
Private void loadconfiguration (idictionary <string, string> initparams)
{
String trytest = string. empty;
// Load the custom configuration 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"];
// Obtain relevant information from the configuration file
If (! String. isnullorempty (configurationmanager. receivettings ["maxfilesizekb"])
{
If (Int. tryparse (configurationmanager. etettings ["maxfilesizekb"], out _ maxfilesize ))
_ Maxfilesize = _ maxfilesize * 1024;
}
If (! String. isnullorempty (configurationmanager. etettings ["maxuploads"])
Int. tryparse (configurationmanager. etettings ["maxuploads"], out _ maxupload );
If (! String. isnullorempty (configurationmanager. receivettings ["filefilter"])
_ Filefilter = configurationmanager. receivettings ["filefilter"];
}
Some other readers can check the code debugging and run it on their own. The following shows the running environment: vs2010 and silverlight4.0.
For more information, see the source code.
Http://files.cnblogs.com/huangyuanfengxue/SilverlightMultiFileUploader.rar