【问题标题】:How to upload Selected Photo from Windows Phone如何从 Windows Phone 上传选定的照片
【发布时间】:2014-04-08 08:59:43
【问题描述】:

我正在尝试用 C# 为 Windows Phone 开发一个应用程序,它基本上将用户选择的图片上传到服务器(例如本地主机)。 这个应用程序的工作原理就像一个 PHP 文件上传脚本,用户在其中选择一个文件,然后将其上传到服务器上所需的目录。

我已经编写了在图片选择器任务的帮助下选择图片的代码。 但是,现在我完全糊涂了。我只是不知道如何处理选定的图片。

这是要求用户选择图片的页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.IO;
using System.Windows.Media.Imaging;


namespace QR_Reader
{
    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;
            }
        }

    }
}

请帮帮我。

这里的txtBX是一个文本框,用来显示选中图片的路径。

【问题讨论】:

  • "我只是不知道如何处理选中的图片。" - 想必你需要写一些上传代码……
  • 是的,没错!但我不知道从哪里开始。

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


【解决方案1】:

您打算上传图片的服务是什么,这是个问题。 Here 是关于如何将其上传到 Imgur 的详尽指南。

一般上传可以这样:

string uploadUrl = "http://uploadserver/upload.php";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uploadUrl);
request.Method = "POST";
request.ContentType = "image/jpeg"; 
request.BeginGetRequestStream((result) =>
{
    using (Stream stream = request.EndGetRequestStream(result))
    {
        stream.Write(bytes, 0, bytes.Length); // your binary data
    }

    request.BeginGetResponse((rResult) => 
    {
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(rResult);

        using (Stream responseStream = response.GetResponseStream())
        {
              // Do something here.
        }
    }
}, null);  

【讨论】:

  • 非常感谢您的回复。实际上,我想将其上传到我的服务器;就像我在 HTML 上传表单中一样。换句话说,在C#中模拟一个多部分文件上传表单是我需要的。我已经尝试了很多,但我不知道该怎么做。请帮帮我。
  • 您的服务器需要有一个端点才能上传。这不是“这是我手机中的文件,以某种方式存储它”的问题。你的服务器有吗?
  • 看,我可以在 PHP 文件上传的帮助下将图片上传到我的服务器。我只想做同样的事情,但在手机上使用应用程序。就是这样。
  • 嗯,是的,如果他/她有 URL,任何人都可以访问存储在服务器上的网页(或文件)。
  • 我的意思是 - 你有一个我们可以测试的公共端点吗?但无论如何,请检查已编辑的帖子。
【解决方案2】:

我是这样做的:

static private async Task<JToken> 
           ImageUploadApiCallAsync(string strApiName, List<KeyValuePair<string, string>> parameterList, Stream imageStream, string strFileName)
        {
            JToken token = null;

            if (!CheckConnection())
            {
                token = @"{
                                'success':false,
                                'message':'No connection',
                                'errorcode':1
                                }";
                return token;
            }

            try
            {
                //Get your api URL
                string strRequestUri = getApiUrlWithApiName(strApiName);

                var httpClient = new HttpClient(new HttpClientHandler());

                using (var content = new MultipartFormDataContent())
                {
                    //I did a stream compression here since I don't want the original size image to upload to my server to reduce space and internet flow.
                    Stream uploadStream = SystemUtil.CompressImageStream(imageStream);

                    content.Add(new StreamContent(uploadStream), "file", strFileName);

                    //Add my api parameters into content
                    foreach (var keyValuePair in parameterList)
                    {
                        content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
                    }
                    //Do PostAsync
                    HttpResponseMessage response = await httpClient.PostAsync(strRequestUri, content);
                    HttpResponseMessage message = response.EnsureSuccessStatusCode();
                    //Get result from server
                    var responseString = await response.Content.ReadAsStringAsync();

                    token = JObject.Parse(responseString);
                }
            }
            catch (Exception e)
            {
                token = @"{
                                'success':false,'message':'" + e.Message + "','errorcode':2}";
            }
            return token;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多