C# 如何在aspx(c)中编写简单的服务器端脚本?

C# 如何在aspx(c)中编写简单的服务器端脚本?,c#,asp.net,C#,Asp.net,我是一名php开发人员,我正在尝试将一些php脚本和重复脚本转换为aspx。我试图制作一个简单的上传脚本,纯服务器端,带有分离的html。此脚本必须将文件保存在服务器中并停止。在本例中,我无法声明函数。这是我的无效脚本,我不知道为什么: <%@ LANGUAGE ='C#' %> <% string filePath = Server.MapPath(Request.QueryString["ax-file-path"]); string fil

我是一名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>

与其尝试将代码内联到页面中,不如将其放到codebehind页面中。这似乎是在回发后放入页面加载的完美代码。

与其尝试在页面中内联代码,不如将其放入代码隐藏页面。这似乎是一个完美的代码,可以在回发后加载页面。

最终我自己找到了解决方案。诀窍是将代码分成两个文件:一个aspx文件和一个aspx.cs。第一个是我从html上传的文件,第二个是处理上传的类。对于那些有相同问题的人,以下是源代码:

upload.aspx:

upload.aspx.cs:


最后我自己找到了解决办法。诀窍是将代码分成两个文件:一个aspx文件和一个aspx.cs。第一个是我从html上传的文件,第二个是处理上传的类。对于那些有相同问题的人,以下是源代码:

upload.aspx:

upload.aspx.cs:


你在调试吗?是否存在特定的异常?是的,我调试,它需要一个},但是是一个奇怪的errorfileBase=fileBase++c.ToString+。+fileExt;这不是问题,我只是纠正,没有什么改变,而不是??这样,错误就出现在第一行,但错误仍然在checkName函数之前}处,您是否调试了它?是否存在特定的异常?是的,我调试,它需要一个},但是是一个奇怪的errorfileBase=fileBase++c.ToString+。+fileExt;这不是问题,我只是纠正,没有什么改变,而不是??错误在第一行,但错误仍然在}前面的checkName函数它只适用于不声明函数,但只是线性代码我刚才看到的可能是我需要的它只适用于不声明函数,但是线性代码我只是在看这可能是我需要的我不太明白你的意思,但我必须把代码和html代码分开,因为html页面可以将文件发布到php文件,也可以发布到aspx文件。我不太明白你的意思,但我必须将代码与html代码分开,因为html页面可以将文件发布到php文件,也可以发布到aspx文件。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        string filePath = Server.MapPath(Request.QueryString["ax-file-path"]);
        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];
                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);
            fileExt = fileExt.Replace(".", "");
            string fileBase     = System.IO.Path.GetFileName(filename);

            //check if legal windows file name
            if(Array.IndexOf(windowsReserved, fileBase)>=0)
            {
                Response.Write(@"{""name"":"""+fileBase+@""",""size"":""0"",""status"":""error"",""info"":""File name not allowed. Windows reserverd.""}");
                return "error";
            }

            //check if is allowed extension
            if (Array.IndexOf(allowExt, fileExt) < 0 && allowExt.Length > 0)
            {
                if (allowExt.Length != 1 || !String.Equals(allowExt[0], ""))
                {
                    Response.Write(@"{""name"":""" + fileBase + @""",""size"":" + allowExt[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.ToString()+")."+fileExt;
                fullPath    = filePath+fileBase;
            }
            return fullPath;
        }
}