在Windows Phone 7应用程序到PHP上传图像文件 [英] Upload image file in Windows Phone 7 Application to PHP

查看:240
本文介绍了在Windows Phone 7应用程序到PHP上传图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从上传图片库(在WP7)的画面并将其保存在服务器上的文件夹中。

I am trying to upload a picture from the Pictures Library (on WP7) and save it in a folder on the server.

在服务器上,我用PHP来接收使用POST方法的文件。
中的PHP代码:

On the server, I'm using PHP to receive the file using POST Method. The PHP Code is:

<?php
$uploads_dir = 'files/'; //Directory to save the file that comes from client application.
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["file"]["tmp_name"];
    $name = $_FILES["file"]["name"];
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>



我已经尝试了一些办法,但他们都只是忽视了。
我已经使用Client.UploadFile方法已经完成了这项工作在Windows窗体应用程序,但它似乎无法在Windows Phone应用程序中使用。

I've already tried some approaches, but they all just seem to fail. I've already done this job in a Windows Forms application using the Client.UploadFile method but it seems that it cannot be used on a Windows Phone Application.

我觉得HttpWebRequest的帮助,右

I think httpwebrequest can help, right?

这是我的C#代码至今:

This is my C# code so far:

public partial class SamplePage : PhoneApplicationPage
    {
        public SamplePage()
        {
            InitializeComponent();
        }

        PhotoChooserTask selectphoto = null;

        private void SampleBtn_Click(object sender, RoutedEventArgs e)
        {
            selectphoto = new PhotoChooserTask();
            selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed);
            selectphoto.Show();
        }

        void selectphoto_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BinaryReader reader = new BinaryReader(e.ChosenPhoto);
                image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
                txtBX.Text = e.OriginalFileName;
            }
        }
    }



我读它的地方是,需要图像转换为字节串,我不知道。
但是,请帮助我。

I read it somewhere that the image is required to be converted to a string of bytes, I don't know for sure. But, please help me.

非常感谢在前进。

推荐答案

我的形象为base64转换(见System.Convert),然后通过邮局汇款:

I would convert the image to base64 (see System.Convert) then transfer via POST:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://mydomain.cc/saveimage.php");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            string postData = String.Format("image={0}", myBase64EncodedImage);   

            // Getting the request stream.
            request.BeginGetRequestStream
                (result =>
                {
                    // Sending the request.
                    using (var requestStream = request.EndGetRequestStream(result))
                    {
                        using (StreamWriter writer = new StreamWriter(requestStream))
                        {
                            writer.Write(postData);
                            writer.Flush();
                        }
                    }

                    // Getting the response.
                    request.BeginGetResponse(responseResult =>
                    {
                        var webResponse = request.EndGetResponse(responseResult);
                        using (var responseStream = webResponse.GetResponseStream())
                        {
                            using (var streamReader = new StreamReader(responseStream))
                            {
                                string srresult = streamReader.ReadToEnd();
                            }
                        }
                    }, null);
                }, null);
        }



saveimage.php应该是这样的:

saveimage.php should look like this:

<?
function base64_to_image( $imageData, $outputfile ) {
    /* encode & write data (binary) */
    $ifp = fopen( $outputfile, "wb" );
    fwrite( $ifp, base64_decode( $imageData ) );
    fclose( $ifp );
    /* return output filename */
    return( $outputfile );
}       

if (isset($_POST['image'])) {
    base64_to_jpeg($_POST['image'], "my_path_to_store_images.jpg");
}
else
    die("no image data found");
?>



注:我没有测试的代码。可能有拼写错误或其他错误。 ,这只是为了说明我怎么会转用POST图片

Note: I have not tested the code. There might be typos or other errors. It's just to illustrate how I would transfer an image using POST.

编辑作为repy您的评论:我没有代码编码为Base64在手,但在这里是你将如何在C#解码base64编码的图像:

Edit as a repy to your comment: I don't have code to encode to base64 at hand but here is how you would decode a base64 encoded image in C#:

byte[] image = Convert.FromBase64String(str);

这篇关于在Windows Phone 7应用程序到PHP上传图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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