1

这是我第一次在 ASP.NET 中做任何事情,所以对于一个有经验的 ASP.NET 开发人员来说,我可能正在做一些非常愚蠢的事情。

情况是这样的:我有很多文件只是命名为 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)
~~~~~~   
4

1 回答 1

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 ...
于 2013-07-29T16:51:11.050 回答