非常简单的Silverlight文件上传示例
我正在寻找Silverlight中的一个非常示例的文件上载代码snipplet/解决方案。通过搜索,我发现了许多控件/项目,但它们都相当复杂;支持多文件上传、文件上传进度、图像重采样等多种类型非常简单的Silverlight文件上传示例,silverlight,file-upload,Silverlight,File Upload,我正在寻找Silverlight中的一个非常示例的文件上载代码snipplet/解决方案。通过搜索,我发现了许多控件/项目,但它们都相当复杂;支持多文件上传、文件上传进度、图像重采样等多种类型 我正在寻找可能的最简单场景,其中包含简短、清晰且易于理解的代码。此代码非常简短且(希望)易于理解: public const int CHUNK_SIZE = 4096; public const string UPLOAD_URI = "http://localhost:55087/FileUploa
我正在寻找可能的最简单场景,其中包含简短、清晰且易于理解的代码。此代码非常简短且(希望)易于理解:
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
}
}
}
public const int CHUNK_SIZE=4096;
公用常量字符串上载\u URI=”http://localhost:55087/FileUpload.ashx?filename={0}&append={1}”;
私有流数据;
私有字符串\u文件名;
私人长
_总的来说;
私人长字节上传;
私有void UploadFileChunk()
{
string uploadUri=“”;//根据文件的第一个块是否设置上载URI的格式
如果(_bytesUpload==0)
{
uploadUri=String.Format(UPLOAD_URI,_fileName,0);//不追加
}
否则如果(_bytesUpload<_bytesTotal)
{
uploadUri=String.Format(UPLOAD_URI,_fileName,1);//追加
}
其他的
{
return;//上传完成
}
字节[]文件内容=新字节[块大小];
_读取数据(文件内容,0,块大小);
WebClient wc=新的WebClient();
wc.OpenWriteCompleted+=新的OpenWriteCompletedEventHandler(wc\u OpenWriteCompleted);
uriu=新Uri(上传Uri);
OpenWriteAsync(u,null,fileContent);
_BytesUpload+=fileContent.Length;
}
void wc_OpenWriteCompleted(对象发送方,OpenWriteCompletedEventArgs e)
{
如果(e.Error==null)
{
object[]objArr=e.UserState作为object[];
byte[]fileContent=objArr[0]作为字节[];
int bytesRead=Convert.ToInt32(objArr[1]);
流输出流=e.结果;
Write(fileContent,0,bytesRead);
outputStream.Close();
如果(\u字节上传<\u字节总数)
{
UploadFileChunk();
}
其他的
{
//上传完成
}
}
}
有关完整的可下载解决方案,请参阅我的博客文章:查看此项目。它允许您用很少的代码行将多个文件上载到服务器。请参阅本文。本文展示了如何使用非常简单的UI上传单个文件,并对每个步骤进行了说明。
为了让将来看到这个答案的人受益,UploadFileAsync或UploadDataAsync在这里可能更合适。OpenWriteAsync非常适合编写流,但它没有将fileContent之类的字节数组作为参数并上传。OpenWriteCompletedEventHandler的意思是“steam现在可以编写了”,而不是“上传完成了”。感谢您的注意,我不知道UploadFileAsync。我做了一些搜索,发现SL2不支持它。。。我将研究版本3是否支持它,并相应地更新代码!你是对的,似乎不是,甚至在3.0中也不是:-(你没有提到服务器上需要一个Web服务才能接收文件=(=)(像我这样的傻瓜不这么认为!我试着上传一个文档和ppt,但上传后,我无法在powerpoint中打开它,这给了我一个错误“演示文稿无法打开”看起来数据已损坏。是否有其他人发现相同的错误?谢谢。