【发布时间】:2017-03-26 02:15:36
【问题描述】:
我有以下剃须刀代码:
<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>
当我单击“加密”按钮时,会调用以下控制器代码:
[HttpPost]
public ActionResult EncryptFile(string uploadedfile)
{
/*process the file without uploading*/
return Json(new { status = "success", message = "Encrypted!" });
}
当我单击加密按钮时,我可以执行此操作,但uploadedfile 字符串始终以null 出现。如何获取所选文件的填充文件路径?请注意,我并没有尝试将其上传到服务器(尽管名称中出现了“已上传”),我只需要文件路径。
编辑
我在 IE 11 中看到以下内容完全显示了文件路径(警报内的部分):
alert($("#encryptfilefield").val());
然而,这不是一个完整的解决方案,而且由于它是一个安全问题,似乎没有解决方案。 谢谢。
【问题讨论】:
-
我删除了我的答案,因为我意识到 HttpPostedFileBase 不允许您在许多浏览器上获得完整路径,只有文件名。这是一个谈论它的问题。 stackoverflow.com/questions/7976998/…
-
只有IE提供了获取文件所在路径的能力;忘了它是怎么做的,但我记得读过它是可能的。
-
@NickG 我明白了,谢谢。幸运的是,我的项目要求是使用 Internet Explorer,但谁知道将来是否会改变(无论是在我这边,还是 IE 的这个特性允许访问文件路径)。
-
只是好奇,你为什么需要客户端机器的图像位置?你用它做什么?
-
Mozilla 怎么样?
标签: c# html asp.net-mvc razor