2016-11-11 168 views
1

我有以下剃刀代碼: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());

然而,這不是一個完整的解決方案,並由於這是一個安全問題,似乎沒有解決方案。 謝謝。

+1

我刪除了我的答案,因爲我意識到HttpPostedFileBase將不允許您在許多瀏覽器上獲取完整路徑,只是文件名。這是一個談論它的問題。 http://stackoverflow.com/questions/7976998/asp-net-mvc-get-full-file-name-of-uploaded-file –

+0

只有IE提供了獲取文件路徑的能力;忘了它是如何完成的,但我記得讀過它是可能的。 –

+0

@NickG我明白了,謝謝。幸運的是,我的項目要求是使用Internet Explorer,但是誰知道這種情況是否將在未來發生變化(無論是在我的末端還是IE的這一功能允許訪問文件路徑)。 – ITWorker

回答

2

更新回答

不幸的是有沒有辦法讓信息始終所有的瀏覽器中,有在這裏有關此主題的多篇文章,結論是瀏覽器不允許它爲安全起見。

我沒有發現,在IE 11,他們做包括.value的財產輸入DOM元素中的路徑,但我不知道這是否在其他版本的作品,它不會在Chrome中工作。

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

不幸的是你可以期望的最好。這裏有一篇文章介紹了一些可以實現一些特定場景的方法。

How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

原來的答案(如何獲取文件路徑「後」到達服務器)

空PARAM問題我想是因爲MVC的元素名稱屬性綁定。

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

並且您的控制器操作被標記爲類型字符串,這不是您的輸入類型。

你可以要麼改變這一點,

[HttpPost] 
public ActionResult EncryptFile(HttpPostedFileBase uploadedfile) 
{ 

,或者嘗試直接從像下面的Request對象抓取文件,你必須保存在某個地方它,你得到它的完整路徑,雖然之前,我不相信只有在你保存之後,你纔會得到源文件的路徑。

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

不幸的是我需要它起源的路徑,而不是它將在保存 – ITWorker

+0

處點擊控制器動作的位置,它只是包裝在HttpPostedFileBase對象中的字節流,除了文件名稱之外,沒有其他的源信息。 –

+0

有沒有一種JavaScript方式來獲取表單的值之前提交到控制器? – ITWorker

相關問題