How does JavaScript and. Net obtain the full path name in the file upload dialog box?

Source: Internet
Author: User

In the past few days, I am working on the mail sending function. Considering uploading files to the server, there will be a lot of files slowly, so the. NET saveas function is not used, but in the form of file streams.

To use a file stream, you need to obtain the absolute path of the File Uploaded by the client. The problem is that due to security settings in each browser, IE7/8/FF/GG, the uploaded file does not show the full path, GG and FF only show the file name, and IE8 will show c: \ fakepath \ test.txt ". This is because of browser security settings.

There are only two ways to solve this problem:

1. browser settings:

Open IE browser --> Internet Options --> Security --> Custom Level --> others --> enable "show file directory path when uploading files to the server"
OK, and then run it again.

2 ,. net background Code also inevitably has such a situation, IE6 will display the full path, while IE8/IE7 display: C: \ fakepath \ test.txt FF/Gg/only display the file name. This is the security setting of the browser, so that the server does not know the path of the client.

3. js obtains the full path:

I have not tested some methods found on the Internet.

Address: http://blog.csdn.net/komodo_d/article/details/4802816

The original article is as follows:

I recently wrote a small website and used the fileupload control to upload files. To obtain the local path of the uploaded file, fileupload1.postedfile is used in the CS code. filename. The results show that only the file name does not contain the path. Remember that the path was obtained in this way before.

So I searched the internet. It turns out that the file path is no longer displayed because the browser of the higher version is set to be secure. You only need to find a way to use js to obtain the path. After testing, we found that. Only use document. getelementbyid ("fileupload1"). Value to obtain the full path in IE6. Only file names can be obtained in IE7, IE8, and Firefox.

Later, I found a solution on the Internet. IE7 and IE8 can use the following code to obtain the file path:

Program code

// Determine the browser type

VaR isie = (document. All )? True: false;

VaR isie7 = isie & (navigator. useragent. indexof ('msie 7.0 ')! =-1 );

VaR isie8 = isie & (navigator. useragent. indexof ('msie 8.0 ')! =-1 );

 

Var file = Document. getelementbyid ("fileupload1 ");

If (isie7 | isie8)

{

File. Select ();

VaR Path = Document. selection. createRange (). text;

Document. selection. Empty ();

}

But there is still no way to get the file path under Firefox. Someone on the internet says that the getasdataurl () method can be used to get the path. I tested it. This method is indeed used to obtain paths, but some paths are encrypted. So I continued to look for other methods...

To obtain the upload file path under Firefox, you must modify the settings first. Enter about: config in the address bar, modify the key value of signed. Applets. codebase_principal_support, and change the value to true. Then use the following code to obtain the file path.

Program code

Try {

Netscape. Security. privilegemanager. enableprivilege ("universalxpconnect ");

}

Catch (e ){

Alert ('change the browser settings ');

Return;

}

VaR fname = Document. getelementbyid ("fileupload1"). value;

Var file = components. classes ["@ mozilla.org/file/local1_1"]

. Createinstance (components. Interfaces. nsilocalfile );

Try {

// Back slashes for Windows

File. initwithpath (fname. Replace (///g ,"////"));

}

Catch (e ){

If (E. Result! = Components. Results. ns_error_file_unrecognized_path) Throw E;

Alert ('file cannot be loaded ');

Return;

}

Alert (file. Path); // obtain the file path

When running the preceding code, a warning is displayed in the browser. Select "yes" to obtain the path.

 

4. There may be other methods.

 

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.