我在寻找一个非常例如文件上传代码snipplet / Silverlight中的解决方案。 做完搜索,我发现许多控制/项目,但他们都是相当复杂; 支持多文件上传,文件上传进度,图像重采样和大量的类。
我在寻找短,清洁,易于理解的代码最简单的可能方案。
我在寻找一个非常例如文件上传代码snipplet / Silverlight中的解决方案。 做完搜索,我发现许多控制/项目,但他们都是相当复杂; 支持多文件上传,文件上传进度,图像重采样和大量的类。
我在寻找短,清洁,易于理解的代码最简单的可能方案。
此代码是很短,(希望)很容易理解:
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
}
}
}
对于一个完整的解决方案下载上看到这个我的博客文章: 在Silverlight文件上传-一个简单的解决方案
看看这个项目http://simpleuploader.codeplex.com/ 。 它允许您将多个文件上传到自己的服务器的代码非常-非常几行。
请参考这篇文章。 本文介绍如何上传单个文件用一个非常简单的用户界面,并解释每一个步骤。 http://aspilham.blogspot.com/2010/04/file-upload-in-chunks-using-silverlight.html