3

I have the following razor code:

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

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.

EDIT

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.

ITWorker
  • 965
  • 2
  • 16
  • 39
  • 1
    I deleted my answer because I realized HttpPostedFileBase will not allow you to get the full path on many browsers, just the filename. Here is a question that talks about it. http://stackoverflow.com/questions/7976998/asp-net-mvc-get-full-file-name-of-uploaded-file – Nick G Nov 11 '16 at 20:01
  • Only IE provides the ability to get the path where the file gets from; forgot how it was done but I do remember reading it was possible. – Brian Mains Nov 11 '16 at 20:07
  • @NickG I see, thanks. Fortunately my project requirement is to use Internet Explorer, but who knows if that will change in the future (both on my end as well as this feature of IE allowing the file path to be accessed). – ITWorker Nov 11 '16 at 20:18
  • Just curious, why do you need the image location from client machine ? What are you doing with it ? – Shyju Nov 11 '16 at 23:02
  • What about Mozilla ? – Pasan Eeriyagama Nov 11 '16 at 23:05
  • @Shyju I am running a native C++ function on the file using a routine that resides in the server, so the assumption is that the web application and files will be on the same system, so that is why I am not concerned about security. – ITWorker Nov 15 '16 at 21:22
  • @PasanIndeewara Firefox does not show the full path. – ITWorker Nov 15 '16 at 21:22

1 Answers1

5

Updated Answer

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.

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.

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

Original Answer (how to get file path "After" it reaches server)

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.

you could either change that to this,

[HttpPost]
public ActionResult EncryptFile(HttpPostedFileBase uploadedfile)
{

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!" });
}
Community
  • 1
  • 1
Dylan Hayes
  • 2,331
  • 1
  • 23
  • 33
  • Unfortunately I need the path of where it originated from, not where it would get saved – ITWorker Nov 11 '16 at 19:49
  • at the point where it hits the controller action it's just a stream of bytes wrapped in a HttpPostedFileBase object, no origin info other than name of file. – Dylan Hayes Nov 11 '16 at 19:55
  • Is there a javascript way to get the field value before the form gets submitted to the controller? – ITWorker Nov 11 '16 at 20:15
  • yeah that could work if after you select the file and before you hit submit if the value is on the input (possibly in the value="[filepath]" attribute), before it get's posted, what you could do is write some javascript or jquery when the button is clicked preventDefault() first, then grab the .val() of that input and ajax post that over to your controller action instead. – Dylan Hayes Nov 11 '16 at 20:27
  • I just tested a little bit, IE renders the full file path on the screen (possibly using the shadow dom or something to that effect) but does not include the file path in the actual main DOM, since it's rendering on the shadow dom, i dont know that you can even get to the full file path on the client side either. you may need to have a manditory file path input box that u force users to fill out or something. – Dylan Hayes Nov 11 '16 at 20:35
  • Thanks, this is probably the closest answer, and I also saw that in IE11 I can get the full file path, but the same code does not work in Firefox (it only showed the file name there). – ITWorker Nov 15 '16 at 21:20
  • @DylanHayes I believe the `enctype='multipart/form-data'` part should go in the form tag check this [answer](https://stackoverflow.com/a/8551621/9098126) – Mina Gerges Apr 12 '20 at 07:59