放弃取客户端路径
纠结了好久怎么去取客服端文件的地址 在网上也找了好久没找到 后来仔细想想 如果我们这般程序猿 能随意知道客户的文件夹 那客户不知任我们 宰割了
么 所以微软就不让我们取 但是有时候我们又并没有恶意的想知道文件路径 唉 所以只能我现在这样将就了 呵呵呵呵
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="uploadFile.aspx.cs" Inherits="uploadFile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" Text="上传" />
</div>
</form>
</body>
</html>
-----------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class uploadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_upload_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{ string path = Server.MapPath("~/C/");
string realyPath = path + FileUpload1.FileName;
//if (IsAllowedExtension(FileUpload1))
//{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
if (IsAllowedExtension(FileUpload1, realyPath))
{
Response.Write("<script>alert(’上传成功’);</script>");
}
// }
else
{
Response.Write("<script>alert(’您只能上传jpg或者gif图片’);</script>");
}
}
else
{
Response.Write("<script>alert(’你还没有选择文件’);</script>");
}
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
public static bool IsAllowedExtension(FileUpload hifile,string path)
{
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{ }
r.Close();
fs.Close();
if (fileclass == "255216" || fileclass == "7173")
{
return true;
}
else
{
// File.Delete(path);
return false;
}
}
}
改良后的东东
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="上传文件.aspx.cs" Inherits="文件上传问题.上传文件" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" onclick="Button1_Click"
Width="99px" />
</div>
</form>
</body>
</html>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace 文件上传问题
{
public partial class 上传文件 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.HasFile)
{
string path = Server.MapPath("~/C/");
string realyPath = path + FileUpload1.FileName;
Stream stream = Request.Files[0].InputStream;
// FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
if (IsAllowedExtension(FileUpload1, realyPath, stream))
{
Response.Write("<script>alert('上传成功');</script>");
}
else
{
Response.Write("<script>alert('您只能上传jpg或者gif图片');</script>");
}
}
else
{
Response.Write("<script>alert('你还没有选择文件');</script>");
}
}
catch (Exception error)
{
Response.Write(error.ToString());
}
}
public static bool IsAllowedExtension(FileUpload hifile, string path, Stream stream)
{
// System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(stream);
string fileclass = "";
byte buffer;
try
{
//读文件头
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{ }
r.Close();
//fs.Close();
//文件头的后缀标识
if (fileclass == "255216" || fileclass == "7173")
{
return true;
}
else
{
return false;
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本