gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:13:11 25 4
gpt4 key购买 nike

很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center .




8年前关闭。




我正在 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
}
}
}

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

关于silverlight - 非常简单的 Silverlight 文件上传示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1622518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com