locked
Get full path of a file from the client RRS feed

  • Question

  • User809588236 posted

    Is there a way to use input = "file" and retrieve the full path of the selected file ? I need to get a list of paths so that I can pass that to a youtube uploader, any other workaround for this please? 

    Thursday, September 24, 2020 6:14 PM

All replies

  • User475983607 posted

    sergiuantal98

    Is there a way to use input = "file" and retrieve the full path of the selected file ? I need to get a list of paths so that I can pass that to a youtube uploader, any other workaround for this please? 

    You are responsible for saving the uploaded file on the web server.  You should know the virtual path as you had to write code that saves the file.  The file location on the client's machine is not accessible from a web server.

    Thursday, September 24, 2020 6:28 PM
  • User1312693872 posted

    Hi,sergiuantal98

    For the sake of the privacy, you can not retrieve the full path of the selected file. But you can upload a list of file in another way, hope this demo can hint you:

    View:

    <form id="form" name="form" action="Index" enctype="multipart/form-data" method="post">
        <div class="buttons">
            <div class="upload-button">
                <input id="files" name="files" type="file" size="1" multiple onchange="uploadFiles('files');" />
            </div>
        </div>
    </form>
    
    @section Scripts
    {
        <script>
            function uploadFiles(inputId) {
                var input = document.getElementById(inputId);
                var files = input.files;
                var formData = new FormData();
    
                for (var i = 0; i != files.length; i++) {
                    formData.append("files", files[i]);
                }
    
                $.ajax(
                    {
                        url: "/Home/Index",
                        data: formData,
                        processData: false,
                        contentType: false,
                        type: "POST",
                        success: function (data) {
                            alert("Files Uploaded!");
                        }
                    }
                );
            }
        </script>
    }

    Controller:

    [HttpPost]
            public async Task<IActionResult> Index(IList<IFormFile> files)
            {
                foreach (IFormFile source in files)
                {
                    string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.Trim('"');
    
                    filename = this.EnsureCorrectFilename(filename);
    
                    using (FileStream output = System.IO.File.Create(this.GetPathAndFilename(filename)))
                        await source.CopyToAsync(output);
                }
    
                return this.View();
            }
    
            private string EnsureCorrectFilename(string filename)
            {
                if (filename.Contains("\\"))
                    filename = filename.Substring(filename.LastIndexOf("\\") + 1);
    
                return filename;
            }
    
            private string GetPathAndFilename(string filename)
            {
                return this.hostingEnvironment.WebRootPath+"\\"+ filename;
            }

    Result:

    Best Regards,

    Jerry Cai

    Friday, September 25, 2020 7:31 AM