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