如何在选择文件时获取文件的完整路径 <input type=‘file’>
<input type="file" id="fileUpload">
<script type="text/javascript">
function getFilePath(){
$('input[type=file]').change(function () {
var filePath=$('#fileUpload').val();
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
但filePath var包含only name所选文件,而不是full path.
我在网上搜索过,但出于安全考虑,似乎浏览器(FF,chrome)只是给出文件名.
有没有其他方法来获取所选文件的完整路径?
我有一个表单上传,但我想传递我的数据库的模型信息,以保存具有不同名称的文件.
这是我的剃刀观点:
@model CertispecWeb.Models.Container
@{
ViewBag.Title = "AddDocuments";
}
<h2>AddDocuments</h2>
@Model.ContainerNo
@using (Html.BeginForm("Uploadfile", "Containers", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<input type='file' name='file' id='file' />
<input type="submit" value="submit" />
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
[HttpPost]
public ActionResult Uploadfile(Container containers, HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"),
containers.ContainerNo);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
Run Code Online (Sandbox Code Playgroud)
模型信息不会传递给控制器.我已经读过我可能需要更新模型,我该怎么做?
我在ASP.NET MVC中上传文件时遇到问题.我的代码如下:
视图:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" />
<input type="submit" />
}
Run Code Online (Sandbox Code Playgroud)
控制器:
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile != null && uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(Server.MapPath("/Temp"), Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
但uploadFile总是返回null.任何人都可以找出原因??
是否可以在asp.net mvc中获取上传文件的完整文件名?![在此处输入图像说明]
更新 数据仅包含文件名,但不包含文件路径!有关详细信息,请参阅附件