Asp.net 从BD下载文件
如何从db而不是从特定路径下载文件? 我正在通过以下方式上载文件:Asp.net 从BD下载文件,asp.net,asp.net-core,Asp.net,Asp.net Core,如何从db而不是从特定路径下载文件? 我正在通过以下方式上载文件: [HttpPost, ActionName("CandidateCreate")] [ValidateAntiForgeryToken] public IActionResult CandidateCreatePost([Bind("Name,Number,Profile,CV,CVID")] Candidate candidate, IFormFile CV) { if (ModelS
[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();
}
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);
}
我能找到的关于下载的所有信息都只有path的
**编辑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"}
};
}
公共异步任务下载(字符串文件名、候选文件、文件CV)
{
filename=candidate.Name;
如果(文件名==null)
返回内容(“文件名不存在”);
var path=path.Combine(Directory.GetCurrentDirectory(),“wwwroot”,文件名);
var memory=newmemoryStream();
使用(var stream=newfilestream(路径,FileMode.Open))
{
等待流。CopyToAsync(内存);
}
记忆位置=0;
返回文件(内存,GetContentType(路径),path.GetFileName(路径));
}
私有字符串GetContentType(字符串路径)
{
var types=GetMimeTypes();
var ext=Path.GetExtension(Path.ToLowerInvariant();
返回类型[ext];
}
私有字典GetMimeTypes()
{
返回新词典
{
{.pdf”,“application/pdf”},
{.doc”,“application/vnd.ms word”},
{.docx”,“application/vnd.ms word”}
};
}
我得到一个错误,因为这是使用路径。。。
我需要一些没有路径atribute的东西(我想)你不能透露你的
ApplicationContext
或你的候选者
类的任何信息,所以我得猜你有什么。但是您需要从数据库中检索记录,并根据问题内容,将文件存储为字节[]
我假设您的ApplicationContext
如下所示:
public class ApplicationContext : DbContext
{
public ApplicationContext(DbContextOptions options) : base(options)
{
}
protected ApplicationContext()
{
}
public DbSet<Candidate> Candidates { get; set; }
}
注意:示例中没有错误检查,因此您必须添加您的部分以确保您拥有有效的CV。
我还建议大家阅读一下如何使用DbContext类从数据库检索数据。我已经设法做到了这一点:
[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();
}
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);
}
控制器上有一个
文件
方法,该方法将接受流
、字节[]
或路径
以将文件下载到浏览器而不是actionResult@SimplyGedNo。结果很好。您需要调用文件(…)
而不是View()
。e、 g.返回文件(p1,“application/octetstream”,CV.Filename)
请参阅答案,以获取返回文件(candidate.CV、GetContentType(path)、path.GetFileName(candidate.CVNAME))上可能出现的错误副本代码>“名称“路径”在当前上下文中不存在。此错误不会导致sesne,因为它存在于GetContentType中。请将path
参数更改为candidate。CVNAME
FWIW,您可以仅从以下位置获取MIME类型:。