C# MVC:如何从文件输入字段获取完整的文件路径?

C# MVC:如何从文件输入字段获取完整的文件路径?,c#,html,asp.net-mvc,razor,C#,Html,Asp.net Mvc,Razor,我有以下代码: <div class="container"> @Html.ValidationSummary(false) @using (Html.BeginForm("EncryptFile", "Encryption", new { returnUrl = Request.Url.AbsoluteUri }, FormMethod.Post, new { @id = "encryptionform", @class = "form-hori

我有以下代码:

  <div class="container">
        @Html.ValidationSummary(false)
        @using (Html.BeginForm("EncryptFile", "Encryption", new { returnUrl = Request.Url.AbsoluteUri }, FormMethod.Post, new { @id = "encryptionform", @class = "form-horizontal" }))
        {

            <div class="form-group">
                @Html.Label("File", new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    <input type="file" id="encryptfilefield" name="uploadedfile" enctype='multipart/form-data'/>
                </div>
            </div>


                    <button type="submit" id="encryptfilebutton">Encrypt</button>
                    <button id="decryptfilebutton" type="button">Decrypt</button>
                    <button id="reencryptfilebutton" type="button">Re-Encrypt</button>

        }
    </div>
我可以在单击加密按钮时点击此操作,但uploadedfile字符串始终为null。如何获取所选文件的填充文件路径?请注意,尽管名称中显示upload,但我没有尝试将其上载到服务器,我只需要文件路径

编辑

我在IE 11中看到,以下内容显示了警报中部件的完整文件路径:

警报$encryptfilefield.val

然而,这并不是一个完整的解决方案,而且由于它是一个安全问题,似乎没有解决方案。 谢谢。

更新答案

不幸的是,没有办法在所有浏览器中获得一致的信息。这里有多篇关于这个主题的帖子,结论是浏览器出于安全目的不允许这样做

我确实发现在IE 11中,它们确实在输入dom元素.value属性中包含路径,但我不知道这在其他版本中是否有效,在chrome中也不起作用

$('input[type=file]').change(function () {
   console.dir(this.value);
   console.dir(this.files[0])
})
不幸的是,这是你所能期望的最好的结果。这里有一篇文章,你可以做一些事情来实现一些非常具体的场景

原始答案如何在文件到达服务器后获取文件路径

我认为空参数问题是因为MVC绑定了元素名属性

 <input type="file" id="encryptfilefield" name="uploadedfile" enctype='multipart/form-data'/>
或者尝试直接从请求对象抓取文件,如下图所示,在获得完整路径之前,您必须将其保存在某个位置。但是,我不相信只有在保存之后,您才能获得文件起源的文件路径

[HttpPost]
public ActionResult EncryptFile(string uploadedfile)
{

    HttpPostedFileBase myfile = Request.Files[0];

    if (file.ContentLength > 0) 
    {
        // extract only the fielname
        var fileName = Path.GetFileName(file.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"),fileName);
       file.SaveAs(path);
     }

      /*process the file without uploading*/
      return Json(new { status = "success", message = "Encrypted!" });
}

不幸的是,我需要的是它的起源路径,而不是它在到达控制器操作时的保存位置它只是一个包裹在HttpPostedFileBase对象中的字节流,除了文件名,没有其他来源信息。在表单提交给控制器之前,是否有javascript方法获取字段值?是的,如果在选择文件之后,在点击提交之前,如果值在输入上,可能在值=[filepath]属性中,在发布之前,您可以做的是,首先单击按钮时编写一些javascript或jquery,然后获取该输入的.val,然后将其发布到控制器操作中。我只是做了一点测试,IE在屏幕上渲染完整的文件路径,可能使用阴影dom或类似的东西,但不包括实际主dom中的文件路径,因为它在阴影dom上渲染,我不知道您甚至可以在客户端获得完整的文件路径。您可能需要一个强制用户填写的文件路径输入框或其他内容。我删除了我的答案,因为我意识到HttpPostedFileBase不允许您在许多浏览器上获取完整路径,只允许获取文件名。这里有一个关于它的问题。只有IE提供了获取文件来源路径的能力;我忘了是怎么做的,但我确实记得读过它是可能的。@NickG我明白了,谢谢。幸运的是,我的项目要求是使用Internet Explorer,但谁知道在我这方面以及IE允许访问文件路径的这一功能将来是否会发生变化。只是好奇,为什么您需要客户端计算机的图像位置?你在用它做什么?Mozilla呢?
[HttpPost]
public ActionResult EncryptFile(HttpPostedFileBase uploadedfile)
{
[HttpPost]
public ActionResult EncryptFile(string uploadedfile)
{

    HttpPostedFileBase myfile = Request.Files[0];

    if (file.ContentLength > 0) 
    {
        // extract only the fielname
        var fileName = Path.GetFileName(file.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"),fileName);
       file.SaveAs(path);
     }

      /*process the file without uploading*/
      return Json(new { status = "success", message = "Encrypted!" });
}