none
skydrive上传提示主体无效,包含多个boundary RRS feed

  • 问题

  • 代码如下:

    String uploadUrl = "https://apis.live.net/v5.0/me/skydrive/files?overwrite=false&access_token=";

    FileOpenPicker picker = new FileOpenPicker();
                picker.FileTypeFilter.Add("*");
                StorageFile file = await picker.PickSingleFileAsync();
                int indexOfUploadfileName = file.Path.LastIndexOf("\\");
                string fileName = file.Path.Substring(indexOfUploadfileName + 1);
                IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
                Stream stream = readStream.AsStream();
                MultipartFormDataContent form = new MultipartFormDataContent();
                StreamContent streamContent = new StreamContent(stream, (int)readStream.Size);
                streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                {
                    FileName = "\"" + fileName + "\"",
                    Name = "\"file\""
                   
                };
                streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                form.Add(streamContent, "file");

    Uri uri = new Uri(uploadUrl+access_token);

                try
                {
                    HttpResponseMessage response = await httpClient.PostAsync(uri, form);
                }
                catch (Exception ex)
                {
                    string x = ex.Message;
                }


    2012年12月11日 6:17

答案

  • FileOpenPicker picker = new FileOpenPicker();
                picker.FileTypeFilter.Add("*");
                StorageFile file = await picker.PickSingleFileAsync();
                int indexOfUploadfileName = file.Path.LastIndexOf("\\");
                string fileName = file.Path.Substring(indexOfUploadfileName + 1);
                IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
                Stream stream = readStream.AsStream();

                StreamContent streamContent = new StreamContent(stream, (int)readStream.Size);

                Uri uri = new Uri(uploadUrl +file.Name+ "?access_token=" + access_token);
               
                try
                {
                    HttpResponseMessage response = await httpClient.PutAsync(uri, streamContent);
                    string res =await response.Content.ReadAsStringAsync();
                   
                }
                catch (Exception ex)
                {
                    string x = ex.Message;
                }

    用put实现了 之前走了弯路

    2012年12月12日 3:40

全部回复

  • {
       "error": {
          "code": "request_body_invalid",
          "message": "The request entity body for multipart form-data POST contains multiple boundaries. Only a single boundary value is permitted."
       }
    }
    2012年12月11日 9:43
  • 我就创建一个MultipartFormDataContent实例,其他的全注释掉,怎么还是提示包含多个boundary?
    2012年12月11日 10:46
  • FileOpenPicker picker = new FileOpenPicker();
                picker.FileTypeFilter.Add("*");
                StorageFile file = await picker.PickSingleFileAsync();
                int indexOfUploadfileName = file.Path.LastIndexOf("\\");
                string fileName = file.Path.Substring(indexOfUploadfileName + 1);
                IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);
                Stream stream = readStream.AsStream();

                StreamContent streamContent = new StreamContent(stream, (int)readStream.Size);

                Uri uri = new Uri(uploadUrl +file.Name+ "?access_token=" + access_token);
               
                try
                {
                    HttpResponseMessage response = await httpClient.PutAsync(uri, streamContent);
                    string res =await response.Content.ReadAsStringAsync();
                   
                }
                catch (Exception ex)
                {
                    string x = ex.Message;
                }

    用put实现了 之前走了弯路

    2012年12月12日 3:40