MVC:如何从文件输入字段获取完整的文件路径? [英] MVC: How to get full file path from file input field?

查看:145
本文介绍了MVC:如何从文件输入字段获取完整的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下剃刀代码:

  <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>

,当我单击加密"按钮时,将调用以下控制器代码:

and the following controller code gets called when I click the Encrypt button:

  [HttpPost]
    public ActionResult EncryptFile(string uploadedfile)
    {
       /*process the file without uploading*/
      return Json(new { status = "success", message = "Encrypted!" });
    }

单击加密按钮时,我可以执行此操作,但是uploadedfile字符串始终以null的形式出现.如何获取所选文件的填充文件路径?请注意,我并没有尝试将其上传到服务器(尽管名称中出现了"uploaded"),我只需要文件路径.

I am able to hit this action when I click the encrypt button, but the uploadedfile string always comes in as null. How can I get the fill filepath of the file that was selected? Please note that I am not trying to upload it to the server (despite "uploaded" appearing in the name), I just need the filepath.

编辑

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

I saw in IE 11 that the following showed the file path fully (the part inside the alert):

alert($("#encryptfilefield").val());

但是,这不是一个完整的解决方案,并且由于存在安全问题,因此似乎没有解决方案. 谢谢.

However this is not a full solution, and it seems there is no solution due to to it being a security issue. Thank you.

推荐答案

更新后的答案

不幸的是,无法在所有浏览器中保持一致的信息.这里有多个关于此主题的帖子,结论是浏览器出于安全目的不允许使用该信息.

Unfortunately there's no way to get that info consistently among all browsers., there's multiple posts on here about this topic and the conclusion is browsers don't allow it for security purposes.

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

I did find that in IE 11 they do include the path within the input dom element .value property, but I don't know if that works in other versions, and it does not work in chrome.

$('input[type=file]').change(function () {
   console.dir(this.value);
   console.dir(this.files[0])
})

不幸的是,这是您可以期望的最好的结果.这是一则帖子,其中包含一些您可以做的事情,它们可能会实现一些非常特定的场景.

Unfortunately that's about the best you can expect. Here's a post that has a couple things you could do to possibly achieve some very specific scenarios.

原始答案(如何获取文件路径到达"服务器后的路径)

我正在考虑的null参数问题是因为MVC绑定在元素名称属性上.

The null param issue I'm thinking is because MVC binds on element name property.

 <input type="file" id="encryptfilefield" name="uploadedfile" enctype='multipart/form-data'/>

,并且您的控制器操作被标记为字符串类型,这与您的输入类型不同.

and your controller action is marked as type string, which is not what your input type is.

您可以将其更改为此,

[HttpPost]
public ActionResult EncryptFile(HttpPostedFileBase uploadedfile)
{

或尝试直接从如下所示的Request对象中获取文件,但是您必须先将其保存在某个位置,然后才能获取它的完整路径,但我不相信您会获得它源自的文件路径,只有在保存之后.

or try grabbing the file straight from the Request object like below, you'd have to save it somewhere before you get the full path of it though, i don't believe you'll get the filepath of where it originated from, only after you save it.

[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!" });
}

这篇关于MVC:如何从文件输入字段获取完整的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆