gpt4 book ai didi

azure - Request.Content.ReadAsMultipartAsync 引发 System.IO 异常

转载 作者:行者123 更新时间:2023-12-04 21:41:24 27 4
gpt4 key购买 nike

我正在将图像上传到 ASP WebAPI 服务,然后将其上传到 windows azure。一切都工作得很好,但突然我开始遇到以下异常:

        System.IO.IOException: The process cannot access the file
'C:\DWASFiles\Sites\Tasteat\Temp\BodyPart_a5c79910-6077-4c24-b814-10fdc0e0b3d4'
because it is being used by another process.

这是抛出异常的代码:

    var provider = new BlobStorageProvider(container);
Trace.TraceInformation("Uploading raw image to blob");
await Request.Content.ReadAsMultipartAsync(provider);
Trace.TraceInformation("Uploading finished");

我知道它是这一行 await Request.Content.ReadAsMultipartAsync(provider); 因为我在日志中看到了它前面的行,但没有看到它后面的行。

有什么想法吗?

几天前一切都运转良好

最佳答案

看来我上面发布的代码实际上保存了本地文件,然后才将其上传到服务器,这会导致错误,但速度也很慢。经过多次尝试,我终于更改为以下解决方案,一切开始工作,而且速度更快!

首先创建一个streamprovider:

public class BlobStorageMultipartStreamProvider : MultipartStreamProvider
{
private readonly string _containerName;
private readonly string _fileName;

public BlobStorageMultipartStreamProvider(string containerName, string fileName)
{
_containerName = containerName;
_fileName = fileName;
}

public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
Stream stream = null;

if (!String.IsNullOrWhiteSpace(_fileName))
{
string connectionString = ConfigurationManager.ConnectionStrings["BlobStorage"].ConnectionString;
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobContainer = blobClient.GetContainerReference(_containerName);
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(_fileName);
stream = blob.OpenWrite();
}
return stream;
}
}

上传代码:

 string fileName = Guid.NewGuid()+".Png";
MultipartStreamProvider provider = new BlobStorageMultipartStreamProvider("container",fileName);
Trace.TraceInformation("Uploading raw image to blob");
await Request.Content.ReadAsMultipartAsync(provider);
Trace.TraceInformation("Uploading finished");

关于azure - Request.Content.ReadAsMultipartAsync 引发 System.IO 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25454638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com