C# 如何从Windows Phone上载所选照片

C# 如何从Windows Phone上载所选照片,c#,windows-phone-7,file-upload,C#,Windows Phone 7,File Upload,我正在尝试用C语言为Windows Phone开发一个应用程序,它基本上是将用户选择的图片上传到服务器(例如,本地主机)。 该应用程序的工作方式与PHP文件上传脚本类似,用户选择一个文件,然后将其上传到服务器上所需的目录 我已经在picture Chooser任务的帮助下编写了选择图片的代码。 但是,现在我完全糊涂了。我只是不知道如何处理所选的图片 这是要求用户选择图片的页面代码: using System; using System.Collections.Generic; using Sys

我正在尝试用C语言为Windows Phone开发一个应用程序,它基本上是将用户选择的图片上传到服务器(例如,本地主机)。 该应用程序的工作方式与PHP文件上传脚本类似,用户选择一个文件,然后将其上传到服务器上所需的目录

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

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

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

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Tasks;
使用System.IO;
使用System.Windows.Media.Imaging;
命名空间QR_读取器
{
公共部分类示例页:PhoneApplicationPage
{
公共样本页()
{
初始化组件();
}
PhotoChooserTask selectphoto=null;
私有无效样本单击(对象发送方,路由目标)
{
选择Photo=新建PhotoChooserTask();
selectphoto.Completed+=新事件处理程序(selectphoto\u Completed);
选择photo.Show();
}
void selectphoto_已完成(对象发送方,PhotoResult e)
{
if(e.TaskResult==TaskResult.OK)
{
BinaryReader=新的BinaryReader(e.ChosenPhoto);
image1.Source=新的位图图像(新的Uri(例如OriginalFileName));
txtBX.Text=e.OriginalFileName;
}
}
}
}
请帮帮我


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

问题是,您计划使用什么服务上传图片是关于如何将其上传到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;
        }
静态私有异步任务
ImageUploadApicallalsync(字符串strApiName、列表参数List、流imageStream、字符串strFileName)
{
JToken令牌=null;
如果(!CheckConnection())
{
令牌=@“{
“成功”:错误,
'message':'No connection',
“错误代码”:1
}";
返回令牌;
}
尝试
{
//获取您的api URL
字符串strequesturi=GetApirlWithApiName(strApiName);
var httpClient=newhttpclient(newhttpclienthandler());
使用(var content=new MultipartFormDataContent())
{
//我在这里做了一个流压缩,因为我不希望原始大小的图像上传到我的服务器上,以减少空间和互联网流量。
Stream uploadStream=SystemUtil.CompressImageStream(imageStream);
添加(新的StreamContent(uploadStream),“文件”,strFileName);
//将我的api参数添加到内容中
foreach(参数列表中的var keyValuePair)
{
添加(新的StringContent(keyValuePair.Value)、keyValuePair.Key);
}
//邮递同步
HttpResponseMessage response=等待httpClient.PostAsync(strRequestUri,内容);
HttpResponseMessage=response.EnsureSuccessStatusCode();
//从服务器获取结果
var responseString=await response.Content.ReadAsStringAsync();
token=JObject.Parse(responseString);
}
}
捕获(例外e)
{
令牌=@“{
'success':false,'message':'“+e.message+”,'errorcode':2}”;
}
返回令牌;
}

“我只是不知道如何处理所选图片。”-大概你需要编写一些上传代码……是的,没错!但是我不知道从哪里开始。非常感谢你的回复。实际上,我想把它上传到我的服务器上;就像我将在HTML上传形式。换句话说,我需要的是用C#模拟一个多部分文件上传表单。我试了很多,但我不知道怎么做。请帮助我。你的服务器需要有一个端点才能上传到那里。这不是“这是我手机上的一个文件,以某种方式存储它”的问题。你的服务器有吗?看,我可以通过PHP文件上传将图片上传到我的服务器。我只想做完全相同的事情,但在手机上的应用程序。就是这样。嗯,是的,任何人都可以访问存储在服务器上的网页(或文件),如果他/她有URL的话。我的意思是-你有一个我们可以测试的公共端点吗?但无论如何,请查看编辑后的帖子。