[英]UploadFileAsync not supported in silverlight 5 (Are you missing assembly reference)
我正在使用Silverlight-5
, VS-2010 Express
和SP-1
並且我是C#初學者,並嘗試在單擊“瀏覽”按鈕時上傳文件。 我的GUI就像這樣http://prntscr.com/34tevq,但是當我嘗試在我的代碼中編寫這一行時
client.UploadFileAsync(filename, fileChunks[index]);
(其中WebClient client = new WebClient();
),然后在UploadFileAsync
下顯示紅線,錯誤是:
'System.Net.WebClient' does not contain a definition for 'UploadFileAsync' and no extension method 'UploadFileAsync' accepting a first argument of type 'System.Net.WebClient' could be found (are you missing a using directive or an assembly reference?)
我的C#代碼是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
namespace shekhar_Final
{
public partial class MainPage : UserControl
{
List<byte[]> fileChunks;
int chunkSize, index;
string filename;
double filesize, senddata;
public MainPage()
{
InitializeComponent();
chunkSize = 4096;
filesize = 0;
index = 0;
senddata = 0;
filename=null ;
fileChunks=null;
}
public void browse_button_click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if ((bool)ofd.ShowDialog())
{
filename = ofd.File.Name;
FileStream fs = ofd.File.OpenRead();
filesize = (double)fs.Length;
textBox1.Text = filename;
index = 0;
senddata = 0;
byte[] file = new byte[fs.Length];
fs.Read(file, 0, file.Length);
ConvertToChunks(file);
prgUpload.Maximum = fileChunks.Count;
prgUpload.Value = 0;
uploadChunks(index);
}
}
private void ConvertToChunks(byte[] imagefile)
{
double totalChunks = Math.Ceiling((double)imagefile.Length / (double)chunkSize);
fileChunks = new List<byte[]>();
for (int i = 0; i < totalChunks; i++)
{
byte[] chunks;
int startIndex = i * chunkSize;
if (startIndex + chunkSize > imagefile.Length)
chunks = new byte[imagefile.Length - startIndex];
else
chunks = new byte[chunkSize];
Array.Copy(imagefile,startIndex,chunks,0,chunks.Length);
fileChunks.Add(chunks);
}
}
private void uploadChunks(int index)
{
WebClient client = new WebClient();
client.UploadFileAsync(filename, fileChunks[index]);
//this UploadFileAsync is not even in sky blue color in my VS code.
}
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
//to be done
}
}
}
我是否缺少大會參考? 如是 ? 那哪個呢? 會很有幫助的,謝謝。
Silverlight的WebClient 沒有名為UploadFileAsync
的方法。 但是,桌面運行時可以。
您應該在WebClient上使用OpenWriteAsync
,然后處理OpenWriteCompleted
事件處理程序並寫入Stream。
或者,您可以找到WebClient的替代解決方案,例如使用較低級別的HttpWebRequest
類或第3方庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.