非常简单的Silverlight文件上传的例子 [英] Very simple Silverlight File Upload example

查看:146
本文介绍了非常简单的Silverlight文件上传的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Silverlight中,我正在寻找一个非常典型的文件上传代码snipplet /解决方案。搜索完成后,我发现了很多控件/项目,但是它们都非常复杂。支持多文件上传,文件上传进度,图片重新采样和大量的类。

我正在寻找简单,干净和容易理解的最简单的方案代码。

解决方案

这段代码非常简短,并且(希望)易于理解:

  public const int CHUNK_SIZE = 4096; 
public const string UPLOAD_URI =http:// localhost:55087 / FileUpload.ashx?filename = {0}& append = {1};
私有流_data;
私有字符串_fileName;
私人长
_bytesTotal;
私人长_bytesUploaded;
private void UploadFileChunk()
{
string uploadUri =; //根据文件的第一个块格式化上传URI
if(_bytesUploaded == 0)
{$ b $ uploadUri = String.Format(UPLOAD_URI,_fileName,0); //不要追加
}
else if(_bytesUploaded< _bytesTotal)
{
uploadUri = String.Format(UPLOAD_URI,_fileName,1); //追加
}
其他
{
return; //上传完成
}

byte [] fileContent = new byte [CHUNK_SIZE];
_data.Read(fileContent,0,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,0,bytesRead);
outputStream.Close();
if(_bytesUploaded< _bytesTotal)
{
UploadFileChunk();
}
else
{
//上传完成
}
}
}

有关完整的可下载解决方案,请参阅我的博文: Silverlight中的文件上传 - 一个简单的解决方案


I'm looking for a very example file upload code snipplet/solution in Silverlight. Having done search I've found numerous controls/projects but all of them were quite complex; supporting multiple file upload, file upload progress, image re-sampling and lots of classes.

I'm looking for the simplest possible scenario with short, clean and easy to understand code.

解决方案

This code is pretty short and (hopefully) easy to understand:

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, _fileName, 1); // append
    }
    else
    {
        return;  // Upload finished
    }

    byte[] fileContent = new byte[CHUNK_SIZE];
    _data.Read(fileContent, 0, 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, 0, bytesRead);
        outputStream.Close();
        if (_bytesUploaded < _bytesTotal)
        {
            UploadFileChunk();
        }
        else
        {
            // Upload complete
        }
    } 
}

For a complete downloadable solution see my blog post on this: File Upload in Silverlight - a Simple Solution

这篇关于非常简单的Silverlight文件上传的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆