Windows Phone 7 如何上传图片到服务器 [英] Windows Phone 7 how to upload picture to server

查看:26
本文介绍了Windows Phone 7 如何上传图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码想将图片上传到服务器,如下所示,但总是失败.你知道为什么吗?

My code wants to upload a picture to a server, as below, but it always fails. Do you know why?

   public static void SendRequest(System.Text.StringBuilder sReq, byte[] sbyteData, Action<UpLoadPicData, int> onEventResponse = null, Action onFinally = null)
    {
        WebClient wc = new WebClient();

        wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
        Uri u = new Uri(sReq.ToString());
        wc.Headers[HttpRequestHeader.ContentLength] = sReq.Length.ToString();
        wc.Headers[HttpRequestHeader.Accept] = "*/*";
        wc.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";

        wc.OpenWriteAsync(u, "POST", sbyteData);
    }

    public static void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            object[] objArr = e.UserState as object[];
            byte[] fileContent = e.UserState as byte[];

           Stream outputStream = e.Result;
           outputStream.Write(fileContent, 0, fileContent.Length);
           outputStream.Flush();
           outputStream.Close();

        }
    }

推荐答案

void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
            bmp.SetSource(e.ChosenPhoto);
            image1.Source = bmp;
            byte[] sbytedata = ReadToEnd(e.ChosenPhoto);
            string s = sbytedata.ToString();
            WebClient wc = new WebClient();
            Uri u = new Uri("url here");
            wc.OpenWriteCompleted+=new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
            wc.OpenWriteAsync(u, "POST", sbytedata);

        }
    }
    public static void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            object[] objArr = e.UserState as object[];
            byte[] fileContent = e.UserState as byte[];

            Stream outputStream = e.Result;
            outputStream.Write(fileContent, 0, fileContent.Length);
            outputStream.Flush();
            outputStream.Close();
            string s = e.Result.ToString(); ;

        }
    }
    public static byte[] ReadToEnd(System.IO.Stream stream)
    {
        long originalPosition = stream.Position;
        stream.Position = 0;

        try
        {
            byte[] readBuffer = new byte[4096];

            int totalBytesRead = 0;
            int bytesRead;

            while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
            {
                totalBytesRead += bytesRead;

                if (totalBytesRead == readBuffer.Length)
                {
                    int nextByte = stream.ReadByte();
                    if (nextByte != -1)
                    {
                        byte[] temp = new byte[readBuffer.Length * 2];
                        Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                        Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                        readBuffer = temp;
                        totalBytesRead++;
                    }
                }
            }

            byte[] buffer = readBuffer;
            if (readBuffer.Length != totalBytesRead)
            {
                buffer = new byte[totalBytesRead];
                Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
            }
            return buffer;
        }
        finally
        {
            stream.Position = originalPosition;
        }
    }

我使用了你的代码......并且它有效......我还将转换为字节[]从图像源附加到这个......图像取自画廊

I used your code... and it worked.. i have also attached the convert to byte[] from image source to this.. the image is taken from gallery

这篇关于Windows Phone 7 如何上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆