SILVERLIGHT正在将文件发送到服务器

SILVERLIGHT正在将文件发送到服务器,silverlight,webclient,Silverlight,Webclient,我正在使用PhotoChooserTask从windows phone选择图像,然后我想将此文件/图像发送到服务器 WebClient有方法UploadFile,但Silverlight中的WebClient没有此方法。 我试图使用几个例子,我发现在这个论坛上,但它不工作。有人能从一开始就帮我做这件事吗?我真的不明白Silverlight是如何工作的。您可以使用Webclient类作为上传文件的工具 private void UploadFile() {

我正在使用PhotoChooserTask从windows phone选择图像,然后我想将此文件/图像发送到服务器

WebClient有方法UploadFile,但Silverlight中的WebClient没有此方法。
我试图使用几个例子,我发现在这个论坛上,但它不工作。有人能从一开始就帮我做这件事吗?我真的不明白Silverlight是如何工作的。

您可以使用Webclient类作为上传文件的工具

private void UploadFile()
        {
            FileStream _data; // The file stream to be read
            string uploadUri;

            byte[] fileContent = new byte[_data.Length]; 
            int bytesRead = _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, new object[] { fileContent, bytesRead }); 
        }

        void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) 
        {
            if (e.Error == null)
            {
              // Upload completed without error
            }
        }
在服务器端,您可以像

 public void ProcessRequest(HttpContext context)
    {
        if (context.Request.InputStream.Length == 0)
            throw new ArgumentException("No file input");
        if (context.Request.QueryString["fileName"] == null)
            throw new Exception("Parameter fileName not set!");

        string fileName = context.Request.QueryString["fileName"];
        string filePath = @HostingEnvironment.ApplicationPhysicalPath + "/" + fileName;
        bool appendToFile = context.Request.QueryString["append"] != null && context.Request.QueryString["append"] == "1";

        FileMode fileMode;
        if (!appendToFile)
        {
            if (File.Exists(filePath))
                File.Delete(filePath);
            fileMode = FileMode.Create;
        }
        else
        {
            fileMode = FileMode.Append;
        }
        using (FileStream fs = File.Open(filePath, fileMode))
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                fs.Write(buffer, 0, bytesRead);
            }
            fs.Close();
        }
    }

希望它能帮助您。

我不理解您的服务器端代码。将接收数据的服务器是否具有该代码?所以有一个小问题:我的serwer是使用php而不是c。我没有使用php。但在c语言中,“服务器端代码”意味着无论何时从客户端调用Home.aspx这样的页面,在服务器端的页面加载上,我们都可以处理上面的“服务器端代码”;其中e.ChoosePhoto是PhotoChooserTask的结果。服务器未从该代码中获取任何请求。请以Filestream\u data=new Filestream path,FileMode.Open的形式打开Filestream;好的,我有FileStream _data=newfilestream.OriginalFileName,FileMode.Open;其中e.OriginalFileName为\\Applications\\Data\\52BD42C5-9796-450A-B62B-028508CF26E2\\Data\\PlatformData\\PhotoChooser-b70f8d7f-3f81-4cc5-8a0e-764892d379ab.jpg,此代码引发异常{尝试访问该方法失败:System.IO.FileStream..ctorSystem.String,System.IO.FileMode}