非常简单的Silverlight文件上传示例

非常简单的Silverlight文件上传示例,第1张

概述我在Silverlight中寻找一个非常示例文件上传代码snipplet /解决方案.完成搜索后,我发现了许多控制/项目,但所有这些都非常复杂;支持多文件上传,文件上传进度,图像重新采样和许多类. 我正在寻找最简单的方案,包括简短,干净且易于理解的代码. 这段代码非常简短(希望)易于理解: public const int CHUNK_SIZE = 4096; public const str 我在Silverlight中寻找一个非常示例的文件上传代码snipplet /解决方案.完成搜索后,我发现了许多控制/项目,但所有这些都非常复杂;支持多文件上传,文件上传进度,图像重新采样和许多类.

我正在寻找最简单的方案,包括简短,干净且易于理解的代码.

解决方法 这段代码非常简短(希望)易于理解:

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

总结

以上是内存溢出为你收集整理的非常简单的Silverlight文件上传示例全部内容,希望文章能够帮你解决非常简单的Silverlight文件上传示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1002770.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存