将base64图像从Android发布到Azure Mobile后端

时间:2015-07-16 15:30:26

标签: android rest azure asp.net-web-api azure-mobile-services

我正在尝试将照片从我的Android客户端上传到我的移动后端,方法是在base64中对图像进行编码并通过我的客户端应用的POST主体发送。照片没有上传到服务器。

我的后端方法:

        [HttpPost]
        [Route("api/addactivity")]
        public IHttpActionResult AddNewMediaActivity([FromBody]string base64String, string caption, string email, string type)
        {
            byte[] f = Convert.FromBase64String(base64String);
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container. 

            CloudBlobContainer container = blobClient.GetContainerReference("photos");

            // Create the container if it doesn't already exist.
            container.CreateIfNotExists();

            container.SetPermissions(
             new BlobContainerPermissions
             {
                 PublicAccess = BlobContainerPublicAccessType.Blob
             });


            string uniqueBlobName = string.Format("photos" + "/" + "photos"+ "_{0}{1}", new Random().Next(1, 999999), new Random().Next(1, 999999));

            // Retrieve reference to a blob named "myblob".
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(uniqueBlobName);

            using (MemoryStream stream = new MemoryStream(f))
            {
                blockBlob.UploadFromStream(stream);
            }
}

我的Android代码:

BitmapFactory.Options options = new BitmapFactory.Options();
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                        options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.URL_SAFE);

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mymobilewebaddress.azure-mobile.net/api/addactivity");

      try {
            // Request parameters and other properties.
            List<NameValuePair> params = new ArrayList<NameValuePair>(2);
            params.add(new BasicNameValuePair("Content-Type", "application/json"));
            params.add(new BasicNameValuePair("ACCEPT", "application/json"));
            params.add(new BasicNameValuePair("X-ZUMO-APPLICATION", mobileServiceAppId));
            params.add(new BasicNameValuePair("base64String",image));
            params.add(new BasicNameValuePair("caption",caption));
            params.add(new BasicNameValuePair("email",email));
            params.add(new BasicNameValuePair("type",type));
            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            //Execute and get the response.
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    // do something useful
                } finally {
                    instream.close();
                }
            }
        }
        catch(Exception e)
        {

        }

但是这不会将照片上传到我的后端。也没有错误。照片根本没有到达后端。

我是否通过POST正确发送了base64字符串?  我做错了什么人?

1 个答案:

答案 0 :(得分:0)

将pic上传到BLOB存储。我搜索了几个小时后得到了它。看看: -

上传照片图片是一个多步骤的过程:

首先拍摄照片,然后将TodoItem行插入包含Azure存储使用的新元数据字段的SQL数据库中。新的移动服务SQL插入脚本要求Azure存储提供共享访问签名(SAS)。该脚本将SAS和blob的URI返回给客户端。客户端使用SAS和blob URI上传照片。

那么什么是SAS?

将数据上传到客户端应用程序内的Azure存储服务所需的凭据是不安全的。而是将这些凭据存储在移动服务中,并使用它们生成共享访问签名(SAS),授予上载新映像的权限。 SAS是一个5分钟到期的凭证,由移动服务安全地返回到客户端应用程序。然后,应用程序使用此临时凭证上传图像。

进一步查询和详细分析。请访问此官方文档https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-upload-data-blob-storage/

相关问题