【问题标题】:Download File from BD从 BD 下载文件
【发布时间】:2018-11-30 16:06:31
【问题描述】:

如何从 db 而不是从某个路径下载文件? 我正在以这种方式上传文件:

[HttpPost, ActionName("CandidateCreate")]
    [ValidateAntiForgeryToken]
    public  IActionResult CandidateCreatePost([Bind("Name,Number,Profile,CV,CVID")] Candidate candidate, IFormFile CV)
    {
        if (ModelState.IsValid)
        {
            if (CV != null)
            {
                if (CV.Length > 0)
                {
                    byte[] p1 = null;
                    using (var fs1 = CV.OpenReadStream())
                    using (var ms1 = new MemoryStream())
                    {
                        fs1.CopyTo(ms1);
                        p1 = ms1.ToArray();

                    }
                    candidate.CVNAME = CV.FileName;
                    candidate.CV = p1;
                    }
                }
            using (var applicationContext = new ApplicationContext())
            {
                candidateRepository.Add(candidate);
                 candidateRepository.SaveChanges();

                return RedirectToAction("Candidate");
            }
        }
        return View();
    }

我能找到的关于下载的一切都只有路径

**EDIT1 **

我试过这个:

        public async Task<IActionResult> Download(string filename, Candidate candidate, IFormFile CV)
    {
        filename = candidate.Name;
        if (filename == null)
            return Content("filename not present");

        var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", filename);

        var memory = new MemoryStream();
        using (var stream = new FileStream(path, FileMode.Open))
        {
            await stream.CopyToAsync(memory);
        }
        memory.Position = 0;
        return File(memory, GetContentType(path), Path.GetFileName(path));
    }
    private string GetContentType(string path)
    {
        var types = GetMimeTypes();
        var ext = Path.GetExtension(path).ToLowerInvariant();
        return types[ext];
    }

    private Dictionary<string, string> GetMimeTypes()
    {
        return new Dictionary<string, string>
        {
            {".pdf", "application/pdf"},
            {".doc", "application/vnd.ms-word"},
            {".docx", "application/vnd.ms-word"}
        };
    }

我收到一个错误,因为这是使用路径... 我需要一些没有路径属性的东西(我认为)

【问题讨论】:

  • 控制器上有一个File 方法,它将接受stream、byte[] 或path 将文件下载到浏览器
  • 而不是actionResult? @SimplyGed
  • 没有。 IActionResult 很好。您需要拨打File(...) 而不是View()。例如return File(p1, "application/octetstream", CV.Filename)
  • 查看this question 的答案示例

标签: asp.net asp.net-core


【解决方案1】:

你没有透露任何关于你的ApplicationContext 或Candidate 课程的信息,所以我将不得不猜测你在那里有什么。但是您需要从数据库中检索记录,并根据问题中的内容将文件存储为byte[]。

我会假设你的 ApplicationContext 看起来像这样:

public class ApplicationContext : DbContext
{
    public ApplicationContext(DbContextOptions options) : base(options)
    {
    }

    protected ApplicationContext()
    {
    }

    public DbSet<Candidate> Candidates { get; set; }
}

基于该假设,您可以使用ApplicationContext 类从数据库中检索记录,并将Candidate.CV 作为byte[] 用于File 方法。

public async Task<IActionResult> Download(Candidate candidate)
{
    var candidate = _applicationContext.Candidates.SingleOrDefault(c => c.CVNAME == candidate.CVNAME);

    return File(candidate.CV, GetContentType(path), Path.GetFileName(candidate.CVNAME));
}
private string GetContentType(string path)
{
    var types = GetMimeTypes();
    var ext = Path.GetExtension(path).ToLowerInvariant();
    return types[ext];
}

private Dictionary<string, string> GetMimeTypes()
{
    return new Dictionary<string, string>
    {
        {".pdf", "application/pdf"},
        {".doc", "application/vnd.ms-word"},
        {".docx", "application/vnd.ms-word"}
    };
}

注意:示例中没有错误检查,因此您必须添加一些以确保您拥有有效的简历。 我还建议阅读如何使用 DbContext 类从数据库中检索数据。

【讨论】:

  • 我在return File(candidate.CV, GetContentType(path), Path.GetFileName(candidate.CVNAME));上遇到错误“当前上下文中不存在名称'path'。此错误不会产生错误,因为它存在于GetContentType中
  • 将path参数改为candidate.CVNAME
【解决方案2】:

我已经设法以这种方式完成这项工作:

  public IActionResult Download(Candidate candidate)
    {
        var applicationcontext = new ApplicationContext();
            var candidate1 = applicationcontext.Candidates.SingleOrDefault(c => c.CVNAME== candidate.CVNAME);
        var contentType = "APPLICATION/octet-stream";
        var filename = candidate1.CVNAME;
        return File(candidate1.CV, contentType, filename);
    }

【讨论】:

  • FWIW,您可以仅从 filename 获取 MIME 类型。
猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 2012-01-29
  • 2019-03-18
  • 2016-07-23
  • 2015-01-11
  • 2017-08-25
  • 2014-08-05
  • 1970-01-01
相关资源
最近更新 更多