問題描述
我有一個(gè)重要的腦筋急轉(zhuǎn)彎.
我想用經(jīng)典的 ASP 打開一個(gè)文件.我正在使用各種變量,因?yàn)槭虑榭赡軙?huì)改變,但結(jié)果是正確的.我知道這一點(diǎn)是因?yàn)槲乙呀?jīng)通過復(fù)制鏈接地址并將其放在我的 URL 中來(lái)測(cè)試結(jié)果.現(xiàn)在的問題是:如果我點(diǎn)擊我的鏈接,它不會(huì)做任何事情.不是刷新,不是重定向.沒有什么.有誰(shuí)知道我做錯(cuò)了什么?
好的,這就是交易.我的文件并不總是本地的,這取決于我所處的環(huán)境.如果我復(fù)制粘貼我的網(wǎng)址的結(jié)果,它會(huì)下載.如果我單擊我的 URL,它不會(huì)響應(yīng).有任何想法嗎?瀏覽器問題?(雖然我已經(jīng)測(cè)試了 5 個(gè)瀏覽器)還是別的什么?我真的被困在這里,互聯(lián)網(wǎng)似乎不在我這邊.
我有 3 個(gè)環(huán)境.這里下面的變量是為了使鏈接有效.我知道該鏈接有效,因?yàn)槲乙呀?jīng)通過復(fù)制對(duì)其進(jìn)行了測(cè)試.是的,它確實(shí)以 file:///
開頭,是的,我確定鏈接是正確的.
這是我的代碼行:
response.write("<td class='tab_kolom2'><a href='"&rootRs("pre_rootpad")&rootRs("rootpad_protocollen")&""&overzichtRs("Formuliernr")&"Uitvoeringsoverzicht.xls' target='_blank' 下載>點(diǎn)擊這里</a></td>")
<小時(shí)>
帶有錯(cuò)誤/鏈接結(jié)果的屏幕截圖
現(xiàn)在我們知道實(shí)際的錯(cuò)誤是什么,可以制定答案了.
<塊引用>不允許加載本地資源
是 Chrome 和其他現(xiàn)代瀏覽器中內(nèi)置的安全異常.措辭可能不同,但在某種程度上,它們都有安全例外來(lái)處理這種情況.
過去您可以覆蓋某些設(shè)置或應(yīng)用某些標(biāo)志,例如
<上一頁(yè)>--disable-web-security --allow-file-access-from-files --allow-file-access在 Chrome 中(參見 https://stackoverflow.com/a/22027002/692942)
<塊引用>它的存在是有原因的
盡管在這一點(diǎn)上值得指出,這些安全異常的存在是有充分理由的,試圖規(guī)避它們并不是最好的主意.
還有一種方法
由于您已經(jīng)可以訪問 Classic ASP,因此您始終可以構(gòu)建一個(gè)為基于網(wǎng)絡(luò)的文件提供服務(wù)的中間頁(yè)面.您可以結(jié)合使用 ADODB.Stream
對(duì)象和 Response.BinaryWrite()
方法來(lái)執(zhí)行此操作.這樣做可確保您的網(wǎng)絡(luò)文件位置永遠(yuǎn)不會(huì)暴露給客戶端,并且由于腳本的靈活性,它可用于從多個(gè)位置和多種文件類型加載資源.
這是一個(gè)基本示例(getfile.asp"):
<%選項(xiàng)顯式Dim s, id, bin, file, 文件名, mimeid = Request.QueryString(id")'id 可以是任何東西,只需將其用作識(shí)別'文件返回.這可能是一個(gè)像這樣的簡(jiǎn)單案例陳述'甚至從數(shù)據(jù)庫(kù)中提取.選擇案例編號(hào)案例TESTFILE1"'文件、mime 和文件名無(wú)論如何都可以建立'必須進(jìn)行硬編碼.文件 = \servershareProjectenProtocollen346Uitvoeringsoverzicht.xls"mime =應(yīng)用程序/vnd.ms-excel";'下載資源時(shí)要顯示的文件名.文件名=Uitvoeringsoverzicht.xls";'假設(shè)其他文件案件 ...結(jié)束選擇如果 Len(file & ">)0 然后設(shè)置 s = Server.CreateObject("ADODB.Stream")s.Type = adTypeBinary 'adTypeBinary = 1 見有用的鏈接"調(diào)用 s.Open()調(diào)用 s.LoadFromFile(file)bin = s.Read()'清理流并釋放內(nèi)存調(diào)用 s.Close()設(shè)置 s = 無(wú)'根據(jù) mime 變量設(shè)置內(nèi)容類型頭Response.ContentType = mime'控制如何使用'內(nèi)容處置 HTTP 標(biāo)頭.使用附件"強(qiáng)制資源'在內(nèi)聯(lián)"時(shí)提示客戶端下載允許資源'下載并在客戶端顯示(用于返回圖片'作為'src'一個(gè)<img>標(biāo)簽).調(diào)用 Response.AddHeader("Content-Disposition", "attachment;filename="; & filename)調(diào)用 Response.BinaryWrite(bin)別的'如果沒有文件,則返回 404.Response.Status = 404 Not Found"萬(wàn)一%>
這個(gè)例子是偽編碼的,因此未經(jīng)測(cè)試.
然后可以像這樣在 <a>
中使用這個(gè)腳本來(lái)返回資源;
<a href="/getfile.asp?id=TESTFILE1">點(diǎn)擊這里</a>
可以進(jìn)一步采用這種方法并考慮(尤其是對(duì)于較大的文件)使用Response.IsConnected
分塊讀取文件以檢查客戶端是否仍然存在并且s.EOS
屬性用于在讀取塊時(shí)檢查流的結(jié)尾.您還可以添加到查詢字符串參數(shù)以設(shè)置是否希望文件返回內(nèi)聯(lián)或提示下載.
有用的鏈接
使用
METADATA
導(dǎo)入 DLL 常量 - 如果您無(wú)法識(shí)別adTypeBinary
,總是比硬編碼1
更好.Content-Disposition:inline"和attachment"有什么區(qū)別? - 有用信息關(guān)于
Content-Disposition
在客戶端上的行為方式.
I've got a major brainteaser.
I want to open a file in classic ASP. I'm using various variables because things can change but the outcome is correct. I know this because I've tested the outcome by copying the linkadress and placing it in my URL. Now the problem: If I click my link it doesn't do anything. Not a refresh, not a redirect. nothing. Does anyone know what I did wrong?
Ok here's the deal. My file isn't always local, it depends on what environment I'm on. If I copy-paste the outcome of my url it does download. If I click my URL it doesn't respond. Any ideas? Browser problem? (although I've tested 5 browsers) Or anything else? I'm really stuck here and the internet does not seem to be on my side.
I've got 3 environments. The variables underneath here are so that the link works. I know the link works because I've tested it by copying. And yes, it does begin with file:///
and yes I'm sure the link is right.
Here's my line of code:
response.write("<td class='tab_kolom2'><a href='"&rootRs("pre_rootpad")&rootRs("rootpad_protocollen")&""&overzichtRs("Formuliernr")&"Uitvoeringsoverzicht.xls' target='_blank' download>Click here</a></td>")
EDIT: Screenshot with error/outcome of link
Now we know what the actual error is can formulate an answer.
Not allowed to load local resource
is a Security exception built into Chrome and other modern browsers. The wording may be different but in some way shape or form they all have security exceptions in place to deal with this scenario.
In the past you could override certain settings or apply certain flags such as
--disable-web-security --allow-file-access-from-files --allow-file-access
in Chrome (See https://stackoverflow.com/a/22027002/692942)
It's there for a reason
At this point though it's worth pointing out that these security exceptions exist for good reason and trying to circumvent them isn't the best idea.
There is another way
As you have access to Classic ASP already you could always build a intermediary page that serves the network based files. You do this using a combination of the ADODB.Stream
object and the Response.BinaryWrite()
method. Doing this ensures your network file locations are never exposed to the client and due to the flexibility of the script it can be used to load resources from multiple locations and multiple file types.
Here is a basic example ("getfile.asp"):
<%
Option Explicit
Dim s, id, bin, file, filename, mime
id = Request.QueryString("id")
'id can be anything just use it as a key to identify the
'file to return. It could be a simple Case statement like this
'or even pulled from a database.
Select Case id
Case "TESTFILE1"
'The file, mime and filename can be built-up anyway they don't
'have to be hard coded.
file = "\servershareProjectenProtocollen346Uitvoeringsoverzicht.xls"
mime = "application/vnd.ms-excel"
'Filename you want to display when downloading the resource.
filename = "Uitvoeringsoverzicht.xls"
'Assuming other files
Case ...
End Select
If Len(file & "") > 0 Then
Set s = Server.CreateObject("ADODB.Stream")
s.Type = adTypeBinary 'adTypeBinary = 1 See "Useful Links"
Call s.Open()
Call s.LoadFromFile(file)
bin = s.Read()
'Clean-up the stream and free memory
Call s.Close()
Set s = Nothing
'Set content type header based on mime variable
Response.ContentType = mime
'Control how the content is returned using the
'Content-Disposition HTTP Header. Using "attachment" forces the resource
'to prompt the client to download while "inline" allows the resource to
'download and display in the client (useful for returning images
'as the "src" of a <img> tag).
Call Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)
Call Response.BinaryWrite(bin)
Else
'Return a 404 if there's no file.
Response.Status = "404 Not Found"
End If
%>
This example is pseudo coded and as such is untested.
This script can then be used in <a>
like this to return the resource;
<a href="/getfile.asp?id=TESTFILE1">Click Here</a>
The could take this approach further and consider (especially for larger files) reading the file in chunks using Response.IsConnected
to check whether the client is still there and s.EOS
property to check for the end of the stream while the chunks are being read. You could also add to the querystring parameters to set whether you want the file to return in-line or prompt to be downloaded.
Useful Links
Using
METADATA
to Import DLL Constants - If you are having trouble gettingadTypeBinary
to be recongnised, always better then just hard coding1
.Content-Disposition:What are the differences between "inline" and "attachment"? - Useful information about how
Content-Disposition
behaves on the client.
這篇關(guān)于文件 URL “不允許加載本地資源"在 Internet 瀏覽器中的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!