如何从Windows Phone上传选定的照片

本文关键字:照片 Windows Phone | 更新日期: 2023-09-27 18:04:06

我正在尝试开发一个应用程序的Windows Phone在c#基本上上传用户选择的图片到服务器(本地主机,例如)。这个应用程序的工作原理就像一个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是一个文本框,用来显示所选图片的路径。

如何从Windows Phone上传选定的照片

你打算用什么服务上传图片,这是个问题。这里是一个关于如何将其上传到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);  

我的做法如下:

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