我正在尝试在C#中开发一个适用于Windows Phone的应用程序,它基本上将用户选择的图片上传到服务器(例如localhost)。 这个应用程序就像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;
}
}
}
}
请帮帮我。
此处,txtBX是一个显示所选图片路径的文本框。
答案 0 :(得分: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);
答案 1 :(得分:0)
我是这样做的:
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;
}