質問

I am using Silverlight-5 ,VS-2010 Express and SP-1 and i am C# beginner and trying to upload a file on Browse button click. My GUI is like this http://prntscr.com/34tevq but when i try to write this line in my code

client.UploadFileAsync(filename, fileChunks[index]); (where WebClient client = new WebClient();) then it gives red line under UploadFileAsync and the error is :

'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?) 

My c# code is :

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
        }
    }
}

Am i missing assembly reference ? If yes ? then which ? Would be a great help, thanks.

役に立ちましたか?

解決

Silverlight's WebClient doesn't have a method called UploadFileAsync. The desktop runtimes do, however.

You should use OpenWriteAsync on WebClient, then handle the OpenWriteCompleted event handler and write to the Stream.

Alternatively you can find an alternative solution to WebClient, such as working with the lower-level HttpWebRequest class or a 3rd party library.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top