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

时间:2012-03-08 21:06:24

标签: c# asp.net

我是一名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的经验,这就是我要问的原因

3 个答案:

答案 0 :(得分:3)

<%@ Page Language="C#">
<script runat="server">
   // your code
</script>

web form

答案 1 :(得分:2)

为什么不把它放在代码隐藏页面中,而不是试图内联页面中的代码。这似乎是在回发后放入页面加载页面的完美代码。

答案 2 :(得分:1)

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

<强> upload.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

<强> upload.aspx.cs:

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;
        }
}