Ripristina il caricamento della foto di Windows Phone
-
14-11-2019 - |
Domanda
Sto cercando di caricare una foto su un'API di riposo in un'applicazione di Windows Phone 7 usando RestSharp per i miei migliori / post.
I parametri post sono i seguenti:
.Photo : La foto, codificata come Multipart / Form-Data
Photo_album_id : Identificatore di un album fotografico esistente, che può essere un evento o un gruppo ALBUMHo creato la mia richiesta, ma ogni volta che ottengo
"{\"details\":\"missing photo parameter\",\"problem\":\"The API request is malformed\"}\n
Il mio parametro fotografico è simile a questo:
."---------------------------- 8CD9BFBAFB3CA00 \ R \ NContent-Disposizione: modulo-dati; nome= \" Nome file \ " ; filename="somefile.jpg \" \ r \ ncontent-type: image / jpg \ r \n\ r \n(alcuni junk binari elencati qui) \ r \n----------- -------------------- 8CD9BFBAFB3CA00 - "
Non sono sicuro che sia un problema con come sto presentando i dati binari per l'immagine (attualmente nel mio evento fototaskcompleted, ho letto il contenuto di e.chosenphoto in un byte [] e passa a a Metodo dell helper per creare i dati del modulo) o se non creo correttamente il modulo.
Sto solo cercando di farlo un semplice possibile, allora posso refactor una volta che so come funziona.
.void ImageObtained(object sender, PhotoResult e) { var photo = ReadToEnd(e.ChosenPhoto); var form = PostForm(photo); var request = new RequestWrapper("photo", Method.POST); request.AddParameter("photo_album_id", _album.album_id); request.AddParameter("photo", form); request.Client.ExecuteAsync<object>(request, (response) => { var s = response.Data; }); } private string CreateBoundary() { return "---------------------------" + DateTime.Now.Ticks.ToString("x"); } private string PostForm(byte[] data) { string boundary = CreateBoundary(); StringBuilder post = new StringBuilder(); post.Append(boundary); post.Append("\r\n"); post.Append("Content-Disposition: form-data; name=\"filename\"; filename=\"somefile.jpg\""); post.Append("\r\n"); post.Append("Content-Type: image/jpg"); post.Append("\r\n\r\n"); post.Append(ConvertBytesToString(data)); post.Append("\r\n"); post.Append("--"); post.Append(boundary); post.Append("--"); return post.ToString(); } public static string ConvertBytesToString(byte[] bytes) { string output = String.Empty; MemoryStream stream = new MemoryStream(bytes); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { output = reader.ReadToEnd(); } return output; }
Soluzione
Hammock for Windows Phone makes this real simple.
You just add the file to the request using the AddFile
method and pass it the photo stream.
var request = new RestRequest("photo", WebMethod.Post);
request.AddParameter("photo_album_id", _album.album_id);
request.AddFile("photo", filename, e.ChosenPhoto);
Altri suggerimenti
Hum are you sure that your PostForm is correct ? The content-* params should be set in the headers of your POST and not in the body ?
var request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Add(HttpRequestHeader.Authorization,"blabla");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";