我已经完成了将文件保存到Server中的文件夹的功能,**我现在正在尝试使用HTML download
从Server取回文件,但是还没有找到正确的方法。文件路径了。
在将文件存储在服务器的文件夹中之后,使用实体框架将文件路径保存在DB中之后,我使用file
从DB中检索了filePath = /VisitReportAttachments/1ea2b64e-545d-4c50-ae7d-eefa7178d310.png
。但是此filePath无法正常工作。
<a href="@file.Path" download="@file.name">Click here to download</a>
//file.Path = /VisitReportAttachments/1ea2b64e-545d-4c50-ae7d-eefa7178d310.png
我收到一个错误:Failed - No file
在Controller的SaveFile代码中查看创建FilePath path
:
private void SaveFile(HttpPostedFileBase file)
{
string serverPath = "\\VisitReportAttachments";
if (file!= null)
{
if (!Directory.Exists(serverPath))
{
Directory.CreateDirectory(serverPath);
}
var fileName = Guid.NewGuid()+ Path.GetExtension(file.FileName);
var path = Path.Combine("\\", new DirectoryInfo(serverPath).Name, fileName);
path = relativePath.Replace(@"\", "/"); //this path is stored to DB
....
//As I mentioned: save file to Server is done. I simply post the code that create the filepath in SQL DB while file is storing to Server*
}
}
FilePath像/VisitReportAttachments/1ea2b64e-545d-4c50-ae7d-eefa7178d310.png
一样存储在数据库中
需要帮助!
答案 0 :(得分:1)
使用Server.MapPath
将filePath映射到正确的路径来找出解决方案。
我没有在HTML视图中使用可下载的链接,而是在Controller中创建了Download函数:
[HttpPost]
[Authorize]
public ActionResult DownloadAttachment()
{
return Json(true);
}
[HttpGet]
public ActionResult Download(Guid? attachmentId)
{
var visitAttachment = _visitAttachmentService.FindOne(x => x.Id == attachmentId);
try
{
var serverPath = Server.MapPath(visitAttachment.Path);
byte[] fileBytes = System.IO.File.ReadAllBytes(serverPath);
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, visitAttachment.AttachmentName);
}
catch
{
return File(Encoding.UTF8.GetBytes(""), System.Net.Mime.MediaTypeNames.Application.Octet, visitAttachment.AttachmentName);
}
}
在视图中调用此方法:
<a href="" onclick="Download('@file.Id');">@file.AttachmentName</a>
<script>
function Download(attachmentId) {
var url = '/Visits/DownloadAttachment';
$.post(url,
{
// FilePath: filePath
},
function (data) {
var response = JSON.parse(data);
window.location = '/Visits/Download?attachmentId=' + attachmentId;
},
"json");
}
</script>
现在可以正常使用了。
答案 1 :(得分:0)
您的private string SaveFile(HttpPostedFileBase file)
方法应返回一个NewFile
模型对象,该对象反映数据库中的实体。 private NewFile SaveFile(HttpPostedFileBase file)
public class NewFile
{
public int NewFileId { get; set; }
public string FileName { get; set; }
public string FilePath { get; set; }
}
保存文件时,您需要执行与以下代码类似的操作:
using (var db = new YourDbContext())
{
var newFile = new NewFile { FileName = fileName, FilePath = path };
var savedFile = db.Add(newFile);
db.SaveChanges();
return savedFile; // here is the object you can return to the view and access
// its properties
}
答案 2 :(得分:0)
private string SaveFile(HttpPostedFileBase file)
{
if (file == null)
return string.Empty;
string saveFolder = "VisitReportAttachments";
string fileName = fileName = Guid.NewGuid() + Path.GetExtension(file.FileName);
string serverFolderPath = AppDomain.CurrentDomain.BaseDirectory + "/" + saveFolder;
string savePath = serverFolderPath + "/" + fileName;
if (!Directory.Exists(serverFolderPath))
Directory.CreateDirectory(serverFolderPath);
file.SaveAs(savePath);
return Url.Content($"~/{saveFolder}/{fileName}");
}
答案 3 :(得分:-1)
您尚未支持文件夹中的文件。 在代码中添加以下行
file.SaveAs(serverPath + file.FileName);
所以您的C#代码会像
private string SaveFile(HttpPostedFileBase file)
{
string serverPath = "\\VisitReportAttachments";
if (file!= null)
{
if (!Directory.Exists(serverPath))
{
Directory.CreateDirectory(serverPath);
}
var fileName = Guid.NewGuid()+ Path.GetExtension(file.FileName);
var path = Path.Combine("\\", new DirectoryInfo(serverPath).Name, fileName);
file.SaveAs(serverPath + file.FileName);
path = relativePath.Replace(@"\", "/");
return path;
}
return string.Empty;
}