从BD下载文件

时间:2018-06-21 10:45:54

标签: asp.net asp.net-core

如何从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();
    }

我所能找到的所有关于下载的内容都只有路径的

**编辑1 **

我已经尝试过:

        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"}
        };
    }

我遇到错误,因为这是使用路径... 我需要没有路径归因的东西(我认为)

2 个答案:

答案 0 :(得分: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类从数据库检索数据的信息。

答案 1 :(得分:0)

我设法做到了这一点:

  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);
    }