-1
由System.IO.File.Create
创建的文件不能像HttpPostedFileBase.SaveAs
创建的文件那样下载。System.IO.File.Create文件无法下载?在mvc 5 asp.net
我的代码:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SaveasMP4(HttpPostedFileBase file)
{
try
{
if (file != null && file.ContentLength > 0)
{
string extension = Path.GetExtension(file.FileName);
var path = Path.Combine(Server.MapPath("~/Data/"), file.FileName);
file.SaveAs(path);
if (!extension.Equals(".mp4"))
{
var ffMpeg = new NReco.VideoConverter.FFMpegConverter();
string tempFileName = path.Substring(0, path.LastIndexOf('.')) + ".mp4";
using (FileStream fileStream = System.IO.File.Create(Path.Combine(Server.MapPath("~/Data/"), tempFileName)))
{
ffMpeg.ConvertMedia(path, fileStream, Format.mp4);
}
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
}
}
}
catch (Exception e)
{
ModelState.AddModelError("", e.Message);
return View();
}
return RedirectToAction("Index");
}
如果该文件是MP4可以下载使用file.SaveAs(路径)。但是,如果文件是其他格式,它使用NReco.VideoConverter成功转换为mp4,无法下载,因为它是由System.IO.File.Create(tempFileName)创建的。只要我可以使用ftp下载。
文件访问权限是否存在问题? 如果在文件访问权限中该怎么办?
什么是'tempFileName'和'filess'? –
那么现在会发生什么?是否有例外?你为什么不使用'fileStream'? –
我已经使用FileStream但无法工作。 当我尝试下载它时显示没有文件。 其实在服务器和我的文件可以通过FTP下载。 –