我有以下剃刀代碼:MVC:如何從文件輸入字段獲取完整文件路徑?
[HttpPost]
public ActionResult EncryptFile(string uploadedfile)
{
/*process the file without uploading*/
return Json(new { status = "success", message = "Encrypted!" });
}
我能打到這個動作時,我點擊加密按鈕:當我點擊加密按鈕
<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
。我如何獲得所選文件的填充文件路徑?請注意,我沒有嘗試將其上傳到服務器(儘管名稱中出現「上傳」),我只需要文件路徑。
編輯
我在IE 11看見下面完全顯示的文件路徑(警報內的部分):
alert($("#encryptfilefield").val());
然而,這不是一個完整的解決方案,並由於這是一個安全問題,似乎沒有解決方案。 謝謝。
我刪除了我的答案,因爲我意識到HttpPostedFileBase將不允許您在許多瀏覽器上獲取完整路徑,只是文件名。這是一個談論它的問題。 http://stackoverflow.com/questions/7976998/asp-net-mvc-get-full-file-name-of-uploaded-file –
只有IE提供了獲取文件路徑的能力;忘了它是如何完成的,但我記得讀過它是可能的。 –
@NickG我明白了,謝謝。幸運的是,我的項目要求是使用Internet Explorer,但是誰知道這種情況是否將在未來發生變化(無論是在我的末端還是IE的這一功能允許訪問文件路徑)。 – ITWorker