【问题标题】:Upload image file in Windows Phone 7 Application to PHP将 Windows Phone 7 应用程序中的图像文件上传到 PHP
【发布时间】:2013-03-31 11:20:25
【问题描述】:

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

在服务器上,我使用 PHP 通过 POST 方法接收文件。 PHP 代码是:

<?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 应用程序上使用。

我认为 httpwebrequest 可以提供帮助,对吧?

到目前为止,这是我的 C# 代码:

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

我在某处读到需要将图像转换为字节串,我不确定。 但是,请帮助我。

非常感谢。

【问题讨论】:

    标签: c# php windows-phone-7 file-upload


    【解决方案1】:

    我会将图像转换为 base64(请参阅 System.Convert),然后通过 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 应该如下所示:

    <?
    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 传输图像。

    编辑作为对您的评论的回复:我手头没有编码为 base64 的代码,但这里是您如何在 C# 中解码 base64 编码的图像:

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

    【讨论】:

    • 嘿,非常感谢!这正是我一直在寻找的。但是,我无法将图像转换为 base64。你能帮我提供适当的代码吗?我会非常感谢你的!!! :-)
    • 你帮了我很多。非常感谢。但是,我仍然希望将其编码为 base64。 :-) 无论如何,谢谢。
    • 来吧,没那么难……好吧,这个函数叫做ToBase64String。不是太有,是吗? ;)
    • 是的,我已经知道了。但是,函数 ToBase64String 不接受图像作为参数。这就是问题所在。
    • 我仍然停留在 base64 编码。你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多