Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code that silverlight uploads with webclient large files

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to write silverlight upload code with webclient large files", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to write silverlight upload code with webclient large files"!

Client:

The copy code is as follows:

/ / /

/ / write data to the stream

/ / /

/ / /

/ / /

Public async static Task Write (string url, Stream clientStream)

{

If (clientStream.Length > 25,1024,1024)

Url + = "& twee1"; / / 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:

The copy code is as follows:

Private void Save ()

{

String data = Context.Request.QueryString ["data"] .Base64StringDecode ("ABC")

If (data.IsNullOrEmpty ())

Return

Var m = JsonConvert.DeserializeObject (data)

If (m = = null)

Return

Var isSplitBlock = Context.Request.QueryString ["t"] = = "1"; / / whether to upload parts

# region saves the file

/ / initialize the directory

String dirPath = Path.Combine (ConfigHelper.UploadPath, m.Dir); / / File save 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 to 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: read the data in blocks after each stream is saved, otherwise multiple blocks will come together, and the data in front of the block stream will be overwritten by the later block stream data.

Pay attention to the process while paying attention to the results

Thank you for your reading, the above is "how to write silverlight upload code with large webclient files" content, after the study of this article, I believe you on how to write silverlight upload code with large webclient files have a deeper understanding of this problem, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report