【发布时间】:2019-06-27 04:19:30
【问题描述】:
我正在构建一个 ASP.NET Web API 项目,该项目需要使用 Azure blob 存储上传 Word 文档。我有一种创建用户配置文件的方法,但不幸的是,我遇到了与 azure blob 存储和上传文档相关的问题。调试时,IFormFile 文档为空,与 azure 的连接也为空。我无法弄清楚断开连接的位置。如果有人知道解决方案,我将不胜感激。下面是我写的代码。
型号
Person Model
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName {get;set;}
public string URL {get;set;}
public string DocName {get;set;}
public string IFormFile[] DocUpload {get;set;}
存储库
public class DocRepo : IDocRepo
{
private CloudStorageAccount storageAccount = null;
private CloudBlobContainer cloudBlobContainer = null;
public IConfiguration _config { get; }
private readonly ILogger _logger;
public DocRepo(ILogger logger)
{
_logger = logger;
}
public void Configure()
{
var AzConnectionString = _config["AzConnectionString"];
if (CloudStorageAccount.TryParse(AzConnectionString, out storageAccount))
{
try
{
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference("folder");
cloudBlobContainer.CreateAsync().GetAwaiter().GetResult();
BlobContainerPermissions permissions = new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
};
cloudBlobContainer.SetPermissionsAsync(permissions).GetAwaiter().GetResult();
}
catch (StorageException ex)
{
_logger.LogError("Error returned from the service: {0}", ex);
}
_logger.LogInformation("Success");
}
else
{
_logger.LogInformation("No Connection");
}
}
public async Task Upload(IFormFile[] docuFile)
{
Person person = new Person();
person.DocUpload = docuFile;
List<string> url = new List<string>();
List<string> docName = new List<string>();
try
{
if (docuFile != null)
{
foreach (var file in docuFile)
{
if (file.ContentType == "application/msword")
{
if (file.Length < 5 * 1400 * 1400)
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
var filename = parsedContentDisposition.FileName.Trim('"');
docName.Add(filename);
filename = Guid.NewGuid().ToString() + "-" + filename;
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(filename);
cloudBlockBlob.Properties.ContentType = file.ContentType;
await cloudBlockBlob.UploadFromStreamAsync(file.OpenReadStream());
url.Add(cloudBlockBlob.Uri.AbsoluteUri);
}
else
{
_logger.LogInformation("5 mb max size allowed");
}
}
else
{
_logger.LogInformation("Only .doc format accepted");
}
}
person.URL = (urls.Count > 0) ? string.Join(" || ", urls) : null;
person.DocName = string.Join(" || ", name);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error uploading");
}
}
界面
public interface IDocRepo
{
void Configure();
Task Upload(IFormFile[] doc);
}
人员控制器
[HttpPost]
public async Task<IActionResult> Create([FromBody] Person person)
{
if (ModelState.IsValid)
{
try
{
_docRepo.CreateConfigure();
await _docRepo.Upload(person.DocUpload);
_context.Person.Add(person);
await _context.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}
return CreatedAtAction("PersonCreated", new { id = person.Id }, person);
}
【问题讨论】:
-
您遇到的错误是什么?
-
嗯,其实是两个错误。第一个是我无法连接到 azure 模拟器,第二个是文件不会上传。调试时它给出一个空值。
-
所以您使用 'usedevelopment=true' 作为连接字符串?您确定您的笔记本电脑上正在运行 azure 模拟器吗?
标签: asp.net azure asp.net-web-api azure-blob-storage