這是我第一次在ASP.NET中做任何事情,所以我可能對有經驗的ASP.NET開發人員做了一件非常愚蠢的事情。ASP.NET File.Copy拋出聲明期望的錯誤
以下是這種情況:我有很多文件被命名爲GUID而沒有文件擴展名。有時我想在網頁瀏覽器中渲染這些內容。如果我用.pdf
文件擴展名手動重命名文件,這一切都可以正常工作。我試圖在ASP.NET中編寫腳本來複制文件以在請求時添加擴展名。這裏是我的代碼:
<script runat="server">
dim guid = HttpContext.Current.Request.QueryString("file")
dim path = HttpContext.Current.Request.PhysicalApplicationPath
dim origin = path + guid
dim destination = origin + ".pdf"
System.IO.File.Copy(origin, destination)
</script>
<html>
<head>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var htmldata = "<embed src='http://myurl.com/<%=guid%>.pdf' style='width:100%; height:100%;'>";
document.getElementById('pdf').innerHTML = htmldata;
});
</script>
</head>
<body>
<span id="pdf"></span>
</body>
</html>
這引發以下錯誤:
Description: An error occurred during the compilation of a resource required to
service this request. Please review the following specific error details and
modify your source code appropriately.
Compiler Error Message: BC30188: Declaration expected.
Source Error:
Line 6: dim destination = origin + ".pdf"
Line 7:
Line 8: System.IO.File.Copy(origin, destination)
Line 9: </script>
Line 10: <html>
C:\path\to\web\root\default.aspx(8) Line: 8
Show Detailed Compiler Output:
c:\windows\system32\inetsrv> REALLY LONG LINE THAT I'LL INCLUDE IF NEEDED
Microsoft (R) Visual Basic Compiler version 10.0.30319.233
Copyright (c) Microsoft Corporation. All rights reserved.
C:\path\to\web\root\default.aspx(8) : error BC30188: Declaration expected.
System.IO.File.Copy(origin, destination)
~~~~~~
你試過我編輯過的代碼嗎?它應該修復第二個編輯問題。只需將文件名移到Page_Load之外;) –
我已經在發佈編輯代碼之前顯然刷新了頁面。我對我的帖子進行了編輯,以確保問題/答案清晰。謝謝! – Chris
另外,使用guid作爲變量名拋出了一個關於「guid是一個類型」或其他東西的錯誤。所以我將變量名改爲了filename而不是guid。 – Chris