Sample code uploaded by silverlight using a webclient large file

Source: Internet
Author: User

Client:
Copy codeThe Code is as follows:
/// <Summary>
/// Write data to the stream
/// </Summary>
/// <Param name = "url"> </param>
/// <Param name = "callback"> </param>
Public async static Task <bool> Write (string url, Stream clientStream)
{
If (clientStream. Length> 25*1024*1024)
Url + = "& t = 1"; // indicates uploading large files
Try
{
Up (url, clientStream );
Return true;
}
Catch {}
Return false;
}
Public async static Task Up (string url, Stream sourceStream)
{
Var wc = new WebClient ();
Byte [] buffer = new byte [25*1024*1024];
Int bufLen = sourceStream. Read (buffer, 0, buffer. Length );
If (bufLen <1)
{
SourceStream. Close ();
Return;
}
Wc. WriteStreamClosed + = (s, e) =>
{
If (sourceStream. CanRead)
Up (url, sourceStream );
Else
SourceStream. Close ();
};
Var serverStream = await wc. OpenWriteTaskAsync (url, "POST ");
ServerStream. Write (buffer, 0, bufLen );
ServerStream. Close ();
}

Server:
Copy codeThe Code is as follows:
Private void Save ()
{
String data = Context. Request. QueryString ["data"]. Base64StringDecode ("ABC ");
If (data. IsNullOrEmpty ())
Return;
Var m = JsonConvert. DeserializeObject <FileUploadModel> (data );
If (m = null)
Return;
Var isSplitBlock = Context. Request. QueryString ["t"] = "1"; // whether to perform multipart upload
# Region save the file
// Initialize the Directory
String dirPath = Path. Combine (ConfigHelper. UploadPath, m. Dir); // file storage Path
If (! Directory. Exists (dirPath ))
Directory. CreateDirectory (dirPath );
// File address
String filePath = Path. Combine (dirPath, m. FileName );
If (! IsSplitBlock)
{
If (File. Exists (filePath ))
File. Delete (filePath );
}
Int bufLen = 0;
Byte [] buffer = new byte [4096];
Using (FileStream fs = new FileStream (filePath, FileMode. OpenOrCreate, FileAccess. ReadWrite ))
{
Fs. Seek (0, SeekOrigin. End );
// Write the original file
Stream sr = Context. Request. InputStream;
While (bufLen = sr. Read (buffer, 0, buffer. Length)> 0)
Fs. Write (buffer, 0, bufLen );
Sr. Close ();
Sr. Dispose ();
// Thumbnail
Try
{
If (! M. NeedThumbnail)
Return;
DirPath = Path. Combine (dirPath, "Small ");
If (! Directory. Exists (dirPath ))
Directory. CreateDirectory (dirPath );
FilePath = Path. Combine (dirPath, m. FileName );
If (File. Exists (filePath ))
File. Delete (filePath );
Using (var pic = GetThumbnail (fs, 300,300 ))
{
Pic. Save (filePath );
}
}
Catch {}
}
# Endregion
# Region Delete the original file
// Delete the original file
If (m. OldFilePath. IsNullOrEmpty ())
{
Return;
}
Try
{
FilePath = Path. Combine (ConfigHelper. UploadPath, m. OldFilePath );
If (File. Exists (filePath ))
File. Delete (filePath );
If (m. NeedThumbnail)
{
FilePath = Path. Combine (ConfigHelper. UploadPath, m. OldThumbnailImagePath );
If (File. Exists (filePath ))
File. Delete (filePath );
}
}
Catch (Exception ex)
{
}
# Endregion
}

Note for multipart upload: after each stream is saved, read the data in the next block. Otherwise, the block stream data before the multiple streams will be overwritten by the subsequent block stream data;
Process-oriented and result-oriented

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.