Windows Phone Photo Upload를 재설정하십시오
-
14-11-2019 - |
문제
Windows Phone에서 REST API에 사진을 업로드하려고 시도하고 있습니다.
게시물 매개 변수는 다음과 같습니다.
사진 : 멀티 파트 / 양식 데이터로 인코딩 된 사진
photo_album_id : 이벤트 또는 그룹 일 수있는 기존 사진 앨범의 식별자 앨범나는 내 요청을 만들었지 만, PergenaCodicetagode 를 되돌릴 때마다
내 사진 매개 변수가 다음과 같습니다 :
"------------------------- 8CD9BFBAF3CA00 \ r \ ncontent-disposition : 양식 데이터; 이름= \"filename \ " ; filename="somefile.jpg \"\ r \ ncontent-type : 이미지 / jpg \ r \n\n(여기에 나열된 일부 바이너리 정크) \ r \n---------- ------------------- 8CD9BFBAFB3CA00 - "
은 이미지의 바이너리 데이터를 제시하는 방법에 대한 문제가 있는지 확실하지 않습니다 (현재 내 PhotoTaskCompleted 이벤트에있는 경우, 나는 e.CosenPhoto의 내용을 바이트 []로 읽고 헬퍼 메소드 양식 데이터를 만드는 데있어) 또는 제가 양식을 올바르게 작성하지 않으면
나는이 일을 가능한 한 간단하게하려고 노력하고 있기 때문에 모든 것이 어떻게 작동하는지 알고 있으면 리팩터를 할 수 있습니다.
.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; }
해결책
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);
다른 팁
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";