简体   繁体   English

如何下载我的文件夹中的文件 c# ASP.NET Web API?

[英]How can I download file in my folder c# ASP.NET Web API?

I need a download file in my C:\fileName .我的C:\fileName中需要一个下载文件。

I am send to fileName this class but it does not working?我被发送到 fileName 此类,但它不起作用? Where is my error?我的错误在哪里?

This is my code:这是我的代码:

// POST: api/Calendar/DownloadFile
[HttpPost]
public HttpResponseMessage DownloadFile(DownloadInput fileName)
{
        var result = new HttpResponseMessage(HttpStatusCode.OK);
        var filePath = HttpContext.Current.Server.MapPath(@"C:\" + fileName);
        var fileBytes = File.ReadAllBytes(filePath);
        var fileMemStream = new MemoryStream(fileBytes);
        result.Content = new StreamContent(fileMemStream);
        var headers = result.Content.Headers;
        headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        headers.ContentDisposition.FileName = fileName;
        headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        headers.ContentLength = fileMemStream.Length;

        return result;
}

This is my error:这是我的错误:

ExceptionType: "System.NotImplementedException"异常类型:“System.NotImplementedException”
Message: "An error has occurred."消息:“发生错误。”

The below is for MVC:以下是 MVC 的:

  public ActionResult DownloadFile(string fileWithFullPath)
    {                
        var fileBytes = System.IO.File.ReadAllBytes(fileWithFullPath);    
        return File(fileBytes, MediaTypeNames.Application.Octet, fileName);
    }

The below is for API:以下是API:

public ActionResult DownloadFile()
{
            var result = new HttpResponseMessage(HttpStatusCode.OK);
            var stream = new FileStream(fullyQualifiedFileName, FileMode.Open);
            result.Content = new StreamContent(stream);
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            result.Content.Headers.ContentLength = stream.Length;

            string input = $"filename=test.pdf";
            ContentDispositionHeaderValue contentDisposition = null;
            if (ContentDispositionHeaderValue.TryParse(input, out contentDisposition))
            {
                result.Content.Headers.ContentDisposition = contentDisposition;
            }

            return this.ResponseMessage(result);
}

Based on what you are showing here, the parameter DosyaAdi is a string and it is not clear what DownloadInput is.根据您在此处显示的内容,参数 DosyaAdi 是一个字符串,不清楚 DownloadInput 是什么。 I would suggest changing the input parameter to a string and also changing the Method to HttpGet.我建议将输入参数更改为字符串,并将方法更改为 HttpGet。 The second suggestion is for convention and probably not the issue.第二个建议是为了惯例,可能不是问题。

// GET: api/Calendar/DownloadFile
[HttpGet]
public HttpResponseMessage DownloadFile(string DosyaAdi)...

good luck祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#EF ASP.NET Web API超时 - 如何调用长时间运行的查询? - C# EF ASP.NET Web API timeout - how can I call a long running query? 下载文件ASP.NET C# - Download file ASP.NET C# 如何在实体框架工作中使用 c# 将任何类型的文本文件转换为 asp.net web api 中的 PDF 文件? - how can i convert any kind of text file into a PDF file in asp.net web api using c# in entity frame work? 如何使用C#在ASP.NET MVC中下载文件? - How to download a file in ASP.NET MVC using C#? 如何使用C#在ASP.NET中下载文件 - How to download a file in asp.net using c# 我试图在 java 中重新创建这个 C# 代码来调用我的 web api make i ASP.NET - Im trying to recreate this C# code in java to call my web api make i ASP.NET 如何强制用户下载文件,即不允许在ASP.NET C#中另存为 - How to force a user to download a file i.e not allowing to save as in ASP.NET C# 如何使用 asp.net C# 将 pdf 生成的文件保存到项目文件夹? - How can save pdf genrated file to project folder using asp.net C#? 我可以在我的 C# ASP.NET MVC 应用程序中将 App_Data 文件夹用作普通文件夹吗? - Can I use App_Data folder as a normal folder in my C# ASP.NET MVC application? 我可以在Windows控制台C#中使用asp.net Web项目文件吗? - Can I use asp.net web project file in windows console c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM