解决方法
这段代码非常简短(希望)易于理解:
public const int CHUNK_SIZE = 4096; public const string UPLOAD_URI = "http://localhost:55087/FileUpload.ashx?filename={0}&append={1}"; private Stream _data; private string _fileName; private long _bytesTotal; private long _bytesuploaded; private void UploadFileChunk() { string uploadUri = ""; // Format the upload URI according to wether the it's the first chunk of the file if (_bytesuploaded == 0) { uploadUri = String.Format(UPLOAD_URI,_fileName,0); // Dont't append } else if (_bytesuploaded < _bytesTotal) { uploadUri = String.Format(UPLOAD_URI,1); // append } else { return; // Upload finished } byte[] fileContent = new byte[CHUNK_SIZE]; _data.Read(fileContent,CHUNK_SIZE); WebClient wc = new WebClient(); wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted); Uri u = new Uri(uploadUri); wc.OpenWriteAsync(u,null,fileContent); _bytesuploaded += fileContent.Length; } void wc_OpenWriteCompleted(object sender,OpenWriteCompletedEventArgs e) { if (e.Error == null) { object[] objArr = e.UserState as object[]; byte[] fileContent = objArr[0] as byte[]; int bytesRead = Convert.ToInt32(objArr[1]); Stream outputStream = e.Result; outputStream.Write(fileContent,bytesRead); outputStream.Close(); if (_bytesuploaded < _bytesTotal) { UploadFileChunk(); } else { // Upload complete } } }
有关完整的可下载解决方案,请参阅我的博文:File Upload in Silverlight – a Simple Solution
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。