【问题标题】:Web API : Read the image send from the appWeb API:读取应用程序发送的图像
【发布时间】:2013-10-17 19:54:30
【问题描述】:

我需要读取从 Ios 应用发送到 Azure 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) };

        }
    }

【问题讨论】:

    标签: c#-4.0 file-upload blob azure-storage asp.net-web-api


    【解决方案1】:

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

    【讨论】:

    • 我们不能像在普通的 ashx 服务处理程序中那样简单地从流中读取文件吗?我只需要读取文件的代码.. 上传到云 blob 不是问题。
    • 您需要使用MultipartStreamProvider。一些例子herehere
    【解决方案2】:

    这是我的代码,

    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;
        }
    

    希望这会对某人有所帮助..

    【讨论】:

    • 图像如何映射到您的 web api 方法?能否请您也显示您的 api 操作签名?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2014-06-19
    • 2018-08-19
    • 2015-08-16
    • 2018-04-27
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多