[英]image upload to server in windows phone8 app
我想從圖庫或實時圖片(相機捕獲任務)中上傳帶有某些參數的圖像; x =“某些字符串”,y =“某些字符串”,z =“某些整數”,並且uploadimage =格式中的文件標簽名稱。
誰能幫幫我嗎?
上載WP8的圖片很麻煩。 我遵循的許多示例已過時,這花了我幾天的時間進行研究,最后使其中的一個得以開展。 有幾種上傳方式:
1)將圖像轉換為字符串,然后可以通過HTTP請求發送圖像轉換后的字符串。 我在Android上使用了這種方法,但尚未在WP中嘗試過這種方法。 2)通過FTP上傳圖像,並通過HTTP上傳文本數據。
您有3種選擇可將任何File
上傳到服務器。
Stream
-推薦方式 ByteArray
String
之后,您可以使用HttpClient
程序包向服務器發出POST request
。 這是通過將FileUpload轉換為Stream來演示它的代碼。
碼:
public async void methodToUploadFile()
{
StorageFile file = await StorageFile.GetFileFromPathAsync("Assets/MyImage.png");
// var fileBytes = await GetBytesAsync(file);
HttpClient client = new HttpClient();
// give the server URI here
Uri requestUri = new Uri("Full Server URI", UriKind.Absolute);
MultipartFormDataContent formdata = new MultipartFormDataContent();
formdata.Add(new StringContent("some string"), "x");
formdata.Add(new StringContent("some string"), "y");
formdata.Add(new StringContent("some integer"), "z");
formdata.Add(new StreamContent(await file.OpenStreamForReadAsync()), "file", "MyImage.png");
// formdata.Add(new ByteArrayContent(fileBytes), "file", "MyImage.png");
// Make a POST request here
var res = await client.PostAsync(requestUri, formdata);
}
希望這可以幫助..!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.