首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >silverlight 5中不支持UploadFileAsync (缺少程序集引用)

silverlight 5中不支持UploadFileAsync (缺少程序集引用)
EN

Stack Overflow用户
提问于 2014-03-28 13:21:47
回答 1查看 100关注 0票数 0

我正在使用Silverlight-5VS-2010 ExpressSP-1,我是C#初学者,并且尝试上传一个文件,点击浏览按钮。我的图形用户界面就像这个http://prntscr.com/34tevq,但是当我试图在代码中写这一行时

然后,client.UploadFileAsync(filename, fileChunks[index]); ( WebClient client = new WebClient();)在UploadFileAsync下给出了红线,错误是:

代码语言:javascript
复制
'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#代码是:

代码语言:javascript
复制
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
        }
    }
}

我缺少程序集参考吗?如果是?那是哪一个?会有很大帮助的,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-28 13:30:54

Silverlight的WebClient 没有,一种叫做UploadFileAsync的方法。然而,桌面运行时是这样的。

您应该在OpenWriteAsync上使用WebClient,然后处理OpenWriteCompleted事件处理程序并写入流。

或者,您可以找到WebClient的另一种解决方案,例如使用较低级别的HttpWebRequest类或第三方库。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22714084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档