2017-08-05 57 views
0

我试图简单地上传一个图像文件到Azure blob存储和文件正在使它在那里,但文件大小比它应该是更大,当我通过浏览器下载文件,它不会作为图像打开。如何正确编码二进制数据通过REST API发送PUT调用

我手动上传了同一个文件,通过Azure界面和文件工作,是22k,通过我的代码上传是29k,并不起作用。

注意:有一点需要注意的是,与此代码一起使用的所有示例仅发送文本文件。也许Encoding.UTF8.GetBytes(requestBody);线正在做呢?

这是我base64编码我的数据

HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase 
byte[] binData; 
using (BinaryReader b = new BinaryReader(hpf.InputStream)) 
    binData = b.ReadBytes(hpf.ContentLength); 

var result = System.Convert.ToBase64String(binData); 
new BlobHelper("STORENAMEHERE", "SECRETKEY").PutBlob("images", "test.jpg", result); 

这是我使用的是把数据是什么,它https://azurestoragesamples.codeplex.com/

public bool PutBlob(string container, string blob, string content) 
{ 
    return Retry<bool>(delegate() 
    { 
     HttpWebResponse response; 

     try 
     { 
      SortedList<string, string> headers = new SortedList<string, string>(); 
      headers.Add("x-ms-blob-type", "BlockBlob"); 

      response = CreateRESTRequest("PUT", container + "/" + blob, content, headers).GetResponse() as HttpWebResponse; 
      response.Close(); 
      return true; 
     } 
     catch (WebException ex) 
     { 
      if (ex.Status == WebExceptionStatus.ProtocolError && 
       ex.Response != null && 
       (int)(ex.Response as HttpWebResponse).StatusCode == 409) 
       return false; 

      throw; 
     } 
    }); 
} 

public HttpWebRequest CreateRESTRequest(string method, string resource, string requestBody = null, SortedList<string, string> headers = null, 
    string ifMatch = "", string md5 = "") 
{ 
    byte[] byteArray = null; 
    DateTime now = DateTime.UtcNow; 
    string uri = Endpoint + resource; 

    HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest; 
    request.Method = method; 
    request.ContentLength = 0; 
    request.Headers.Add("x-ms-date", now.ToString("R", System.Globalization.CultureInfo.InvariantCulture)); 
    request.Headers.Add("x-ms-version", "2009-09-19"); 

    if (headers != null) 
    { 
     foreach (KeyValuePair<string, string> header in headers) 
      request.Headers.Add(header.Key, header.Value); 
    } 

    if (!String.IsNullOrEmpty(requestBody)) 
    { 
     request.Headers.Add("Accept-Charset", "UTF-8"); 

     byteArray = Encoding.UTF8.GetBytes(requestBody); 
     request.ContentLength = byteArray.Length; 
    } 

    request.Headers.Add("Authorization", AuthorizationHeader(method, now, request, ifMatch, md5)); 

    if (!String.IsNullOrEmpty(requestBody)) 
     request.GetRequestStream().Write(byteArray, 0, byteArray.Length); 

    return request; 
} 
+1

猜测它与大小无关,而是与请求中的标题相关。你可以添加调用'CreateRESTRequest()'的代码吗? – vorou

+0

@vorou OP更新 – user3953989

+0

从22k到29k的大小增加大约为3:4,所以看起来你在某处是两次Base64编码。 – SBS

回答

1

的,我相信这个问题是怎么您将二进制数据转换为字符串,然后将其转换回字节数组。

在将二进制数据转换为字符串时,您使用的是System.Convert.ToBase64String,但是在从字符串中获取字节数组时,您使用的是Encoding.UTF8.GetBytes。这很可能是导致不匹配的原因。

请更改下面的代码行:

byteArray = Encoding.UTF8.GetBytes(requestBody); 

byteArray = System.Convert.FromBase64String(requestBody); 

,并应解决这个问题。

+0

我实际上可以通过删除UTF编码和Base64编码来实现这一功能。所提供的示例是硬编码的,只能用于显然的文本文件。虽然我认为你必须在通过http发送之前进行编码 – user3953989

相关问题