Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过.Net web api从azure blob下载文档_C#_Asynchronous_Asp.net Web Api_Azure Blob Storage - Fatal编程技术网

C# 通过.Net web api从azure blob下载文档

C# 通过.Net web api从azure blob下载文档,c#,asynchronous,asp.net-web-api,azure-blob-storage,C#,Asynchronous,Asp.net Web Api,Azure Blob Storage,我很难弄清楚到底出了什么问题。我的应用程序点击api获取文档。正在发生的是下载开始,但挂起。最终它会结束(要么出错,要么完全结束),但当我试图打开(比如说pdf)时,我会得到一个“无法打开pdf”或类似的结果。它在本地工作 我的控制器: [Route("api/listing/attachment")] [HttpGet] public async Task<IHttpActionResult> GetAttachmentAsync(string fileName)

我很难弄清楚到底出了什么问题。我的应用程序点击api获取文档。正在发生的是下载开始,但挂起。最终它会结束(要么出错,要么完全结束),但当我试图打开(比如说pdf)时,我会得到一个“无法打开pdf”或类似的结果。它在本地工作

我的控制器:

 [Route("api/listing/attachment")]
    [HttpGet]
    public async Task<IHttpActionResult> GetAttachmentAsync(string fileName)
    {
        var attachment = await _repository.GetAttachmentAsync(fileName);
        var response = HttpContext.Current.Response;
        response.Clear();
        response.ContentType = "application/x-download";
        var removePath = fileName.Substring(fileName.IndexOf("/", fileName.IndexOf("/", StringComparison.Ordinal) + 1, StringComparison.Ordinal) + 1);
        response.AddHeader("content-disposition", string.Format("attachment; filename={0}", removePath));
        response.AddHeader("content-length", attachment.Length.ToString());
        response.BinaryWrite(attachment);
        response.Flush();
        return null;
    }
[路径(“api/列表/附件”)]
[HttpGet]
公共异步任务GetAttachmentAsync(字符串文件名)
{
var attachment=await_repository.GetAttachmentAsync(文件名);
var response=HttpContext.Current.response;
response.Clear();
response.ContentType=“应用程序/x-download”;
var removePath=fileName.Substring(fileName.IndexOf(“/”,fileName.IndexOf(“/”,StringComparison.Ordinal)+1,StringComparison.Ordinal)+1);
AddHeader(“内容处置”,string.Format(“附件;文件名={0}”,removePath));
AddHeader(“content-length”,attachment.length.ToString());
响应。二进制写入(附件);
response.Flush();
返回null;
}
我的存储库:

public async Task<byte[]> GetAttachmentAsync(string fileName)
    {
        var container = _blobClient.GetContainerReference(_containerName);
        var blockBlob = container.GetBlockBlobReference(fileName);
        using (var memoryStream = new MemoryStream())
        {
            await blockBlob.DownloadToStreamAsync(memoryStream);
            return memoryStream.ToArray();
        }
    }
public异步任务GetAttachmentAsync(字符串文件名)
{
var container=_blobClient.GetContainerReference(_containerName);
var blockBlob=container.GetBlockBlobReference(文件名);
使用(var memoryStream=new memoryStream())
{
等待blockBlob.下载到StreamAsync(memoryStream);
返回memoryStream.ToArray();
}
}

尝试将控制器代码更改为:

[Route("api/listing/attachment")]
[HttpGet]
public async Task<HttpResponseMessage> GetAttachmentAsync(string fileName)
{
    var attachment = await _repository.GetAttachmentAsync(fileName);
    var removePath = fileName.Substring(fileName.IndexOf("/", fileName.IndexOf("/", StringComparison.Ordinal) + 1, StringComparison.Ordinal) + 1);

    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(attachment)
    };
    result.Content.Headers.ContentDisposition =
        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
    {
        FileName = removePath
    };
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    return result;
}
[路径(“api/列表/附件”)]
[HttpGet]
公共异步任务GetAttachmentAsync(字符串文件名)
{
var attachment=await_repository.GetAttachmentAsync(文件名);
var removePath=fileName.Substring(fileName.IndexOf(“/”,fileName.IndexOf(“/”,StringComparison.Ordinal)+1,StringComparison.Ordinal)+1);
var结果=新的HttpResponseMessage(HttpStatusCode.OK)
{
内容=新的ByteArrayContent(附件)
};
result.Content.Headers.ContentDisposition=
新System.Net.Http.Headers.ContentDispositionHeaderValue(“附件”)
{
FileName=removePath
};
result.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
返回结果;
}

尝试将控制器代码更改为:

[Route("api/listing/attachment")]
[HttpGet]
public async Task<HttpResponseMessage> GetAttachmentAsync(string fileName)
{
    var attachment = await _repository.GetAttachmentAsync(fileName);
    var removePath = fileName.Substring(fileName.IndexOf("/", fileName.IndexOf("/", StringComparison.Ordinal) + 1, StringComparison.Ordinal) + 1);

    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(attachment)
    };
    result.Content.Headers.ContentDisposition =
        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
    {
        FileName = removePath
    };
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    return result;
}
[路径(“api/列表/附件”)]
[HttpGet]
公共异步任务GetAttachmentAsync(字符串文件名)
{
var attachment=await_repository.GetAttachmentAsync(文件名);
var removePath=fileName.Substring(fileName.IndexOf(“/”,fileName.IndexOf(“/”,StringComparison.Ordinal)+1,StringComparison.Ordinal)+1);
var结果=新的HttpResponseMessage(HttpStatusCode.OK)
{
内容=新的ByteArrayContent(附件)
};
result.Content.Headers.ContentDisposition=
新System.Net.Http.Headers.ContentDispositionHeaderValue(“附件”)
{
FileName=removePath
};
result.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
返回结果;
}

您没有返回任何响应。。。您应该返回一些实现
IHttpActionResult
的内容,因为您没有返回任何响应。。。您应该返回一些实现
IHttpActionResult
的东西,我对您的代码做了一个更改,它工作得非常好。我将签名更改为
public async Task GetAttachmentAsync(string fileName)
我无法向您投票,但这正是我所需要的。谢谢你,所以muchI对你的代码做了一个修改,它工作得很好。我将签名更改为
public async Task GetAttachmentAsync(string fileName)
我无法向您投票,但这正是我所需要的。多谢各位