Ich versuche, einige Bilder + einige Metadaten an einen Server per HTTP-Post aus einer Windows-Store-App senden, aber stecken bleiben, wenn Sie versuchen, die Daten in den Beitrag tatsächlich aufzunehmen. Es kann nicht so ausgeführt werden, wie Sie dies in einer Windows Forms-App oder ähnlichem aufgrund der Änderungen an der Store-App-API erreichen würden.senden Byte-Array von HTTP-POST in Store-App
Ich bekomme den Fehler.
cannot convert source type byte[] to target type System.Net.Http.httpContent
dies jetzt ist offensichtlich, weil es zwei verschiedene Arten gibt, die nicht implizit gegossen werden kann, aber es ist im Grunde, was ich suche der Lage sein, zu tun. Wie mache ich meine Byte-Array-Daten in das bekommen httpContent geben, damit ich es in den folgenden Aufruf enthalten
httpClient.PostAsync(Uri uri,HttpContent content);
hier meine volle Upload-Methode:
async private Task UploadPhotos(List<Photo> photoCollection, string recipient, string format)
{
PhotoDataGroupDTO photoGroupDTO = PhotoSessionMapper.Map(photoCollection);
try
{
var client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
client.DefaultRequestHeaders.Add("Upload", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
// POST action_begin
const string actionBeginUri = "http://localhost:51139/PhotoService.axd?action=Begin";
HttpResponseMessage response = await client.GetAsync(actionBeginUri);
response.EnsureSuccessStatusCode();
string responseBodyAsText = await response.Content.ReadAsStringAsync();
string id = responseBodyAsText;
////
// POST action_upload
Uri actionUploadUri = new Uri("http://localhost:51139/PhotoService.axd?action=Upload&brand={0}&id={1}&name={2}.jpg");
var metaData = new Dictionary<string, string>()
{
{"Id", id},
{"Brand", "M3rror"}, //TODO: Denne tekst skal komme fra en konfigurationsfil.
{"Format", format},
{"Recipient", recipient}
};
string stringData = "";
foreach (string key in metaData.Keys)
{
string value;
metaData.TryGetValue(key, out value);
stringData += key + "=" + value + ",";
}
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteData = encoding.GetBytes(stringData);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, actionUploadUri);
// send meta data
// TODO get byte data in as content
HttpContent metaDataContent = byteData;
HttpResponseMessage actionUploadResponse = await client.PostAsync(actionUploadUri, metaDataContent);
actionUploadResponse.EnsureSuccessStatusCode();
responseBodyAsText = await actionUploadResponse.Content.ReadAsStringAsync();
// send photos
// TODO get byte data in as content
foreach (byte[] imageData in photoGroupDTO.PhotosData)
{
HttpContent imageContent = imageData;
actionUploadResponse = await client.PostAsync(actionUploadUri, imageContent);
actionUploadResponse.EnsureSuccessStatusCode();
responseBodyAsText = await actionUploadResponse.Content.ReadAsStringAsync();
}
////
// POST action_complete
const string actionCompleteUri = "http://localhost:51139/PhotoService.axd?action=Complete";
HttpResponseMessage actionCompleteResponse = await client.GetAsync(actionCompleteUri);
actionCompleteResponse.EnsureSuccessStatusCode();
responseBodyAsText = await actionCompleteResponse.Content.ReadAsStringAsync();
////
}
catch (HttpRequestException e)
{
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
Doesn müssen die Binärdaten * serialisiert * werden? Wahrscheinlich möchten Sie es auch * streamen *, anstatt es auf einmal hochzuladen. http://stackoverflow.com/questions/19005991/webapi-how-to-handle-images kann helfen. – bytefire