2013-10-10 28 views
0

我需要读取从Ios应用程序发送到Azure Web API的文件。Web API:读取从应用程序发送的图像

我需要上传到blob并保持Uri。

任何人都可以建议我接受文件发送的代码。

/// <summary> 
    ///This is to upload the image file for the user profile. 
    /// </summary> 
    ///<param name="userRegId"> </param> 
    ///<returns></returns> 
    [HttpPost] 
    public Response ImageUpload(int userRegId) 
    { 
     try 
     { 
      var response = new Response(); 
      if (!Request.Content.IsMimeMultipartContent()) 
      { 
       response.status = CommonHandler.GetInvalidStatus(); 
      } 
      else 
      { 

       //here i want to read the file and upload it to blob. 


       string fileName =Request.Files[0].FileName;//unable to do this. Here i want to know ho to read the file 
       string uniqueBlobName = string.Format("{0}/{1}", constants.pdf, fileName); 
       CloudBlobClient blobStorage = cloudClasses.CreateOrGetReferenceOfBlobStorage(constants.pdf); 
       CloudBlockBlob blob = cloudClasses.ClouBblockBlobPropertySetting(blobStorage, uniqueBlobName, ".pdf"); 
       blob.UploadFromStream(Request.Files[0].InputStream);//unable to do this. Here i want to know ho to read the file 
       response.status = CommonHandler.GetSuccessStatus(); 
      } 
      return response; 
     } 
     catch (Exception ex) 
     { 
      _logger.LogError(Log4NetLogger.Category.Exception, "Error in : APIController.ImageUpload :>> Exception message: " + ex.Message); 
      return new Response { status = CommonHandler.GetFailedStatus(ex.Message) }; 

     } 
    } 

回答

1

姚黄林写了一篇关于如何生成由Azure blob存储支持的Web API文件服务的博客文章。你可以找到它here

+0

不能我们只需从流中读取该文件作为我们如何在正常的ashx服务处理程序中执行。我只需要代码来读取文件..上传到云blob不是一个问题。 –

+1

您需要使用[MultipartStreamProvider](http://msdn.microsoft.com/en-us/library/system.net.http.multipartstreamprovider(v = vs.108).aspx)。一些例子[这里](http://stackoverflow.com/questions/14475248/multipartmemorystreamprovider-filename)和[这里](http://forums.asp.net/t/1842441.aspx)。 –

+0

是的,我已经完成了你所说的和张贴的代码... –

3

这里是我的代码,

var streamProvider = new MultipartMemoryStreamProvider(); 
       Request.Content.ReadAsMultipartAsync(streamProvider); 
       foreach (var content in streamProvider.Contents) 
       { 
        if (content != null) 
        { 
         if (content.Headers.ContentDisposition.FileName != null) 
         { 
          var fileName = 
           content.Headers.ContentDisposition.FileName.Replace("\"", string. 
                           Empty); 
          Stream stream = content.ReadAsStreamAsync().Result; 
          CloudBlobContainer blobStorage = BlobHandler.GetBlobStorage("profileimage"); 
          CloudBlockBlob blob = BlobHandler.BlobPropertySetting(blobStorage, 
                        Guid.NewGuid().ToString().ToLower() + 
                        fileName); 
          blob.UploadFromStream(stream); 
          response = ProfileHandler.ImageUpdate(userRegId, blob.Uri); 
         } 
        } 
       } 

对于blobHandler

public static CloudBlobContainer GetBlobStorage(string cloudBlobContainerName) 
    { 
     CloudBlobContainer container; 
     try 
     { 
      var storageAccount = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString"); 
      CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 
      container = blobClient.GetContainerReference(cloudBlobContainerName); //profile 
      container.CreateIfNotExist(); 
      var permissions = container.GetPermissions(); 
      permissions.PublicAccess = BlobContainerPublicAccessType.Container; 
      container.SetPermissions(permissions); 
     } 
     catch (Exception ex) 
     { 
      Logger.LogError(Log4NetLogger.Category.Exception, "Error in : BlobHandler.GetBlobStorage :>> Exception message: " + ex.Message); 
      throw; 
     } 
     return container; 
    } 
    public static CloudBlockBlob BlobPropertySetting(CloudBlobContainer cloudBlobClientReferenceName, string blobContentName) 
    { 
     CloudBlockBlob blob = cloudBlobClientReferenceName.GetBlockBlobReference(blobContentName); 

     //blob.Properties.ContentType = contentType; 
     return blob; 
    } 

希望这会帮助别人..

+0

如何映射到你的web api方法?你能不能展示你的api动作签名? – InTheWorldOfCodingApplications

相关问题