silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了非常简单的Silverlight文件上传示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

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

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

解决方法

这段代码@R_237_10584@(希望)易于理解

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
_bytes@R_599_10586@l; 
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 < _bytes@R_599_10586@l)
    {
        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 < _bytes@R_599_10586@l)
        {
            UploadFileChunk();
        }
        else
        {
            // Upload complete
        }
    } 
}

有关完整的可下载解决方案,请参阅我的博文:File Upload in Silverlight – a Simple Solution

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。