标签: fileresult

使用WebAPI HttpResponseMessage时下载文件提示

我的API中有一个返回HttpResponseMessage的方法:

    [HttpGet, HoodPeekAuthFilter]
    public HttpResponseMessage GlobalOverview()
    {
        try
        {
            StatsRepo _statsRepo = new StatsRepo();

            string file = _statsRepo.IncidentData().AsCSVString();

            if (file == null)
            {
                return Request.CreateResponse(HttpStatusCode.NoContent);
            }

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StringContent(file);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = "GlobalOverview.csv";

            return result;
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError);
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我的MVC Web Apllication中,我有一个需要调用API并返回文件的控制器Action:

    [Authorize]
    [HttpGet]
    public HttpResponseMessage GlobalOverview()
    {
        HttpResponseMessage file = new HttpResponseMessage();
        using (HttpClient httpClient = new HttpClient())
        {
            httpClient.DefaultRequestHeaders.Authorization = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc httpresponse fileresult asp.net-web-api

2
推荐指数
1
解决办法
9533
查看次数

如何在MVC中查看PDF文档而不直接下载?

我有一个点击它的链接,一个 HTML 页面将被转换为 PDF 文档,然后将此 PDF 文件返回给用户。

HTML代码:

<li><a href='@Url.Action("GetHTMLPageAsPDF", "Transaction", new { empID = employee.emplID })'>ViewReceipt</a></li>
Run Code Online (Sandbox Code Playgroud)

后面的代码:

<li><a href='@Url.Action("GetHTMLPageAsPDF", "Transaction", new { empID = employee.emplID })'>ViewReceipt</a></li>
Run Code Online (Sandbox Code Playgroud)

问题是当这个文件直接返回下载到用户计算机时,我想向用户显示这个PDF文件,然后如果他愿意他可以下载它。

我怎样才能做到这一点?

c# asp.net-mvc html-to-pdf fileresult

2
推荐指数
1
解决办法
6484
查看次数

具有特定文件名和Safari或Chrome的ASP.NET MVC FileResult

我遇到FileResult返回具有特定文件名的文件的问题.在数据库中,文件名只是ID +扩展名(例如:456789.mp3)

这段代码

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return File(fs, "application/octet-stream", "myfile.mp3");
Run Code Online (Sandbox Code Playgroud)

除Webkit浏览器(Chrome,Safari)外,在每个浏览器中都能正常运行.Chrome和Safari会将文件作为原始文件名(456789.mp3)接收.当我添加标题时

Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.mp3");
Run Code Online (Sandbox Code Playgroud)

Safari接收文件为myfile.mp3attach(注意"附加"附加到扩展名?),但Chrome接收此文件为myfile.mp3,附加(它附加",附加"到扩展名)

有没有人遇到过这种问题?

谢谢

safari asp.net-mvc google-chrome fileresult

1
推荐指数
1
解决办法
6124
查看次数

为什么此操作会在客户端产生空文件?

为什么这个动作导致客户端的空文件?


public FileResult download()
{

    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);

    FileStreamResult fs = new FileStreamResult(stream, "text/plain");
    fs.FileDownloadName = "file.txt";

    writer.WriteLine("this text is missing !!! :( ");

    writer.Flush();
    stream.Flush();

    return fs;                  
}

asp.net-mvc memorystream streamwriter fileresult

1
推荐指数
1
解决办法
2071
查看次数

FileResult 内容长度不匹配

您好,我使用这篇博文中的代码:

https://blog.stephencleary.com/2016/11/streaming-zip-on-aspnet-core.html

为了使用 .Net core 传输 zip 文件。我成功了,但由于我下载 zip 文件时没有在响应中添加内容长度标头,因此它不会在 chrome 中显示下载进度。因为我提前知道 zip 文件大小,所以我实际上可以使用 SetHeadersAndLog 方法设置内容长度标头 https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.internal.fileresultexecutorbase .setheadersandlog?view=aspnetcore-2.0

但是当我这样做时出现以下错误:

System.InvalidOperationException: Response Content-Length mismatch: too many bytes written (144144633 of 144144627)

知道为什么响应的长度与 zip 文件的长度不同吗?这是提供该文件的代码:

     this._httpContext.Response.ContentType = "application/octet-stream";
            this._httpContext.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
            this._httpContext.Response.ContentLength = estimatedFileSize;

            FileCallbackResult result = new FileCallbackResult(new MediaTypeHeaderValue("application/octet-stream"), estimatedFileSize, async (outputStream, _) =>
            {
                using (ZipArchive zip = new ZipArchive(outputStream, ZipArchiveMode.Create, false))
                {
                    foreach (string filepath in Directory.EnumerateFiles(existingDirectory.FullName, "*.*", SearchOption.AllDirectories))
                    {
                        string relativepath = filepath.Replace(existingDirectory.FullName + "\\", string.Empty);

                        ZipArchiveEntry zipEntry …
Run Code Online (Sandbox Code Playgroud)

c# http content-length fileresult .net-core

1
推荐指数
1
解决办法
6287
查看次数

使用FileResult MVC4下载/打开Excel文件不起作用

我试图使用FileResult对象在Excel中的本地磁盘上打开Excel文件.当我单击要打开的文件时,它会下载名为与我的ActionResult(WTF?)相同的文件,当我单击下载的文件时,它会显示"选择程序"窗口.如果我选择Excel,它会打开它,但我缺少什么使它下载为excel文件并打开它没有额外的步骤?下面是我打开文件的switch语句.谢谢

public ActionResult GetFile(string path)
    {

        string extension = new FileInfo(path).Extension;
        if (extension != null || extension != string.Empty)
        {
            switch (extension)
            {
                case ".pdf":
                    return File(path, "application/pdf");
                case ".txt":
                    return File(path, "application/plain");
                case ".jpeg":
                    return File(path, "application/jpeg");
                case ".doc":
                    return File(path, "application/msword");
                case ".docx":
                    return File(path, "application/msword");
                case ".xls":
                    return File(path, "application/msexcel");
                case ".xlsx":
                    return File(path, "application/msexcel");
                default:
                    return File(path, "application/octet-stream");
            }
        }
        return View("Index");
    }
Run Code Online (Sandbox Code Playgroud)

file-io fileresult asp.net-mvc-4

0
推荐指数
1
解决办法
1万
查看次数