Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

javascript - Load a pdf with a chinese filename from url

I have a local pdf and I try load this pdf in an iframe by the path of the file.

The file has a name 明星.pdf and the path is like this: http://localhost:8080/path-here/明星.pdf

When I try open the pdf file, the browser do this request and fails (404): http://localhost:8080/path-here/%E6%98%8E%E6%98%9F.pdf

The problem only occurs with pdfs that contains chinese characteres.

Any suggestion?

PS: I'm using javascript.

Edit:

Example:

//there's a http:// in front, I can't put more links in post
var url = 'localhost:8080/path-here/teste.pdf' 
var url2 = 'localhost:8080/path-here/明星.pdf'

window.open(url,"_blank") //works fine
window.open(url2,"_blank") //fails
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Javascript can handle most non-english characters, but you'll probably need to encode them for the web browser to recognise them. Try specifying the webpage (server?)'s charset as UTF-8

    <head>
    <meta charset="UTF-8">
    </head>

You'll likely also need to prepare your string for the webbrowser, I suggest using encodeURIcomponent() or encodeURI(). Here's an explanation of the function https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent. Hope that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...