如何使用php webservice使用POST方法将图像上传到wp7中的服务器?

时间:2011-11-09 13:43:12

标签: silverlight windows-phone-7

我正在尝试从Windows Phone 7中的PhotoChooser中选择上传图像文件,但它没有上传。我正在使用以下代码。

     void UploadFile(string fileName, Stream data)
    {
        UriBuilder ub = new  UriBuilder("http://webservice.php?uid=" + Constants.UserId);

        WebClient webClient = new WebClient();
        webClient.Headers["Content-Type"] = "multipart/form-data; boundary=7794b1e4-2134-41a3-b8ab-ff5aff9710fd";

        webClient.OpenWriteCompleted += (sender, e) =>
        {
            System.Diagnostics.Debug.WriteLine("START");
            PushData(data, e.Result);
            System.Diagnostics.Debug.WriteLine("END");
            e.Result.Close();
            data.Close();
        };
        webClient.WriteStreamClosed += (sender, e) =>
        {
            System.Diagnostics.Debug.WriteLine("WriteStreamClosed");
        };
        webClient.OpenWriteAsync(ub.Uri);
    }

    private void PushData(Stream input, Stream output)
    {
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        Byte[] bytes = encoding.GetBytes("--7794b1e4-2134-41a3-b8ab-ff5aff9710fd\r\n"
            + "Content-Disposition: form-data; name=\"uploadFile\"; filename=\"test.jpg\"\r\n"
            + "Content-Type:Image/jpeg\r\n\r\n");
        output.Write(bytes, 0, bytes.Length);
        int dd = 0;
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
        {
            output.Write(buffer, 0, bytesRead);
            dd += bytesRead;

            System.Diagnostics.Debug.WriteLine("WRITE: " + dd);
        }
        bytes = encoding.GetBytes("\r\n--7794b1e4-2134-41a3-b8ab-ff5aff9710fd--\r\n");
        output.Write(bytes, 0, bytes.Length);
    }

有谁能告诉我上面的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

请参阅以下链接,它对您有所帮助。

problem with uploading file

答案 1 :(得分:1)

观看此视频网络广播,它将引导您使用WP7中的网络通讯

AT&T Beginners Web programming for Windows Phone webcast

此外还有另一篇文章谈论WP7的通知,其中包括将图片上传到图片主机的代码,然后使用该URL发送通知图块更新,因此它显示了发布图像并从响应中检索数据。 / p>

The puppet who has cut his strings

希望这有帮助