简体   繁体   中英

Get full file path including file's name from an uploaded file in C#

I have this web application project which requires a better user-interface and I am doing it by C#. I already have my html file with JS done but I need some data from user. In my JS embedded in HTML file, I used the code below to find the file on local driver and get the data from that excel file and then put all these data into an array.

var excel = new ActiveXObject("Excel.Application"); var excel_file = excel.Workbooks.Open(file1); var excel_sheet = excel.Worksheets("Sheet1");

However, the "file1" you see above seems to require a full name path,say "C:\\test.xls" in my case. I am new to C# and just built a button on my form design, by clicking the button, I seem to be able to browse my local file.

      private void button1_Click(object sender, EventArgs e)
    {
        int size = -1;
        DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
        if (result == DialogResult.OK) // Test result.
        {
            string file = openFileDialog1.FileName;
            try
            {
                string text = File.ReadAllText(file);
                size = text.Length;
            }
            catch (IOException)
            {
            }
            System.Diagnostics.Process.Start(file);



        }
        Console.WriteLine(size); // <-- Shows file size in debugging mode.
        Console.WriteLine(result); // <-- For debugging use.
    }

So, my question:

How can I get this kind of full file path of an uploaded file in C# ? And furthermore, it would be awesome if some one can tell me how to get this value into my javascript or HTML!

Thank you in advance

You won't be able to depend on getting the full path. In the end, the browser only needs to multi-part encode the physical file and submit it as a form post (of sorts). So once it has it's location and has encoded it -- it's done using it.

It's considered a security risk to expose the file structure to Javascript/HTML (ie., the internet).

Just an update.

I used another logic and it worked as expected. Instead of getting absolute file path , I managed to open the file , save it as somewhere and make my html call find that file path no matter what.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM