【问题标题】:UploadFileAsync not supported in silverlight 5 (Are you missing assembly reference)Silverlight 5 中不支持 UploadFileAsync(您是否缺少程序集参考)
【发布时间】:2014-05-07 23:43:56
【问题描述】:

我正在使用Silverlight-5VS-2010 ExpressSP-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
        }
    }
}

我是否缺少程序集参考?如是 ?那么哪个?会很有帮助,谢谢。

【问题讨论】:

    标签: c# silverlight web-applications webclient silverlight-5.0


    【解决方案1】:

    Silverlight 的 WebClient doesn't have 一个名为 UploadFileAsync 的方法。但是,桌面运行时可以。

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

    或者,您可以找到 WebClient 的替代解决方案,例如使用较低级别的 HttpWebRequest 类或第 3 方库。

    【讨论】:

    • 感谢您的回复,但您个人建议我用什么来上传 c# 中的文件和 xml 在 SILVERLIGHT 5 中创建的 GUI?
    猜你喜欢
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 2016-10-08
    • 2020-04-18
    相关资源
    最近更新 更多