我是一名 php 开发人员,我正在尝试将一些 php 脚本(重复脚本)转换为 aspx。我正在尝试制作一个简单的上传脚本,纯服务器端(带有单独的 html)。此脚本必须将文件保存在服务器中并停止。在此示例中,我无法声明函数。这是我不工作的脚本,我不知道为什么:
<%@ LANGUAGE ='C#' %>
<%
string filePath = Server.MapPath(Request.QueryString["ax-file-path"]);
string fileName = Request.QueryString["ax-file-name"];
string allowExtstr = Request.QueryString["ax-allow-ext"];
string[] allowExt = allowExtstr.Split('|');
if (!System.IO.File.Exists(filePath) && filePath.Length > 0)
{
System.IO.Directory.CreateDirectory(filePath);
}
if (!System.IO.File.Exists(thumbPath) && thumbPath.Length > 0)
{
System.IO.Directory.CreateDirectory(thumbPath);
}
if(Request.Files.Count>0)
{
for (int i = 0; i < Request.Files.Count; ++i)
{
HttpPostedFile file = Request.Files[i];
if(file.FileName!="")
{
string fullPath = checkFilename(file.FileName, allowExt, filePath);
file.SaveAs(fullPath);
Response.Write(@"{""name"":"""+System.IO.Path.GetFileName(fullPath)+@""",""size"":""0"",""status"":""uploaded"",""info"":""File/chunk uploaded""}");
}
}
}
public string checkFilename(string filename, string[] allowExt, string filePath)
{
string[] windowsReserved = new string[] {"CON", "PRN", "AUX", "NUL","COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
string[] badWinChars = new string[] {"<", ">", ":", @"\", "/", "|", "?", "*"};
for (int i = 0; i < badWinChars.Length; i++)
{
filename.Replace(badWinChars[i], "");
}
string fileExt = System.IO.Path.GetExtension(filename);
string fileBase = System.IO.Path.GetFileName(filename);
//check if legal windows file name
if(windowsReserved.Contains(fileBase))
{
Response.Write(@"{""name"":"""+fileBase+@""",""size"":""0"",""status"":""error"",""info"":""File name not allowed. Windows reserverd.""}");
return "error";
}
//check if is allowed extension
if(!allowExt.Contains(fileExt) && allowExt.Length>0)
{
Response.Write(@"{""name"":"""+fileBase+@""",""size"":""0"",""status"":""error"",""info"":""Extension "+fileExt+@" not allowed.""}");
return "error";
}
string fullPath = filePath+fileBase;
int c=0;
while(System.IO.File.Exists(fullPath))
{
c++;
fileBase= fileBase+"("+c+")."+fileExt;
fullPath = filePath+fileBase;
}
return fullPath;
}
%>
和简单的 html 代码:
<form action="upload.aspx" method=post enctype=......>
<input type=file />
</form>
我应该如何以正确的方式编写这段代码?
编辑:我只是想要一些帮助。如果这不是编写 aspx c# 的良好形式,请告诉我该怎么做,而不是说我编写的 aspx 代码很差。我是一名 c#、php 开发人员,没有 aspx 方面的经验,这就是我问的原因
最佳答案
<%@ Page Language="C#">
<script runat="server">
// your code
</script>
关于c# - 如何在 aspx (c#) 中编写一个简单的服务器端脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9624943/