2013-07-29 46 views
1

這是我第一次在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) 
~~~~~~ 
+0

你試過我編輯過的代碼嗎?它應該修復第二個編輯問題。只需將文件名移到Page_Load之外;) –

+0

我已經在發佈編輯代碼之前顯然刷新了頁面。我對我的帖子進行了編輯,以確保問題/答案清晰。謝謝! – Chris

+0

另外,使用guid作爲變量名拋出了一個關於「guid是一個類型」或其他東西的錯誤。所以我將變量名改爲了filename而不是guid。 – Chris

回答

0

我覺得代碼是正確的,但我沒有看到任何這函數內。我想您是這個Page_Load事件中:

<script runat="server"> 
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     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) 
    End Sub 
</script> 

編輯:如果您想訪問filename變量,需要在Page_Load外面去,是這樣的一個公共變量:

Public filename As String = HttpContext.Current.Request.QueryString("file") 
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... 
+0

我沒有任何稱爲Form1的東西。這會繼續嗎?我改了一下我的代碼。我將編輯該問題以顯示此內容。 – Chris

+0

將其更改爲Page_Load,即可使用。 –

相關問題