如何在aspx(c#)中编写一个简单的服务器端脚本
本文关键字:一个 脚本 简单 服务器端 aspx | 更新日期: 2023-09-27 18:27:16
我是一名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>
web表单
与其尝试在页面中内联代码,为什么不把它放在codeehind页面中呢。这似乎是在回发后放入页面加载的完美代码。
最后我自己找到了解决方案。诀窍是将代码分为两个文件:一个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;
}
}