求一个附件上传的代码

enasp 2009-01-10 05:04:55
1、指定文件大小 1M右右
2、要能指定文件类型,*.rar *.doc *.jpg *.bmp


3、上传完后并能显示出来

多谢路过的XDJM!!
...全文
187 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cookies10wen 2009-01-12
  • 打赏
  • 举报
回复
顶 2楼的OK
enasp 2009-01-12
  • 打赏
  • 举报
回复
怎么读出来啊!
enasp 2009-01-12
  • 打赏
  • 举报
回复
关键是用gridview怎么一一对应读出来

我的思路是把路径写入数据库,然后用GIRIDVIEW直接显示出来
chinacn101 2009-01-12
  • 打赏
  • 举报
回复
我也来个 自己写的比较简单
但很受用 呵呵
http://www.iyedu.cn/bbs/Show.Asp?ID=101
enasp 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maple2008 的回复:]
upFile.aspx

HTML code<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upFile.aspx.cs" Inherits="Web.admin.imgLinks.upFile" %>

<!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 id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">

[/Quote]

你这个非常好啊,问题我想把存放的路径写入数据库怎么弄?
enasp 2009-01-12
  • 打赏
  • 举报
回复
存放指定路径中,原后怎么按ID读出来??如果只把路径放入数据库,只接读数据库的路径就能读出来

我是用gridview

product表:

id user pic



bool fileok = false;
string path = Server.MapPath("~/temp/");
if(this.FileUpload1.HasFile)
{
string fileexception = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
string[] allowedexception = { ".gif", ".bmp", ".png", ".jpg", ".rar" };
for(int i=0;i<allowedexception.Length;i++)
{
if(fileexception==allowedexception[i])
fileok=true;
}

}



T13yiyong 2009-01-11
  • 打赏
  • 举报
回复
存放在一个指定的 文件夹中, 放在数据库中效率很低的


flyerwing 2009-01-10
  • 打赏
  • 举报
回复
up.
cngothic 2009-01-10
  • 打赏
  • 举报
回复
mark
enasp 2009-01-10
  • 打赏
  • 举报
回复
如果是doc rar 就是显示链接啊
cqdyh 2009-01-10
  • 打赏
  • 举报
回复
1,2 上页都说得较为轻楚了.
3, 上传完后并能显示出来? 显示什么出来?
*.rar *.doc *.jpg *.bmp
这几种格式全然不同, 要显示呢. rar 还得解压,由于rar算法是专利,得使用它的exe
而 doc,jpg,bmp 又是要显示什么呢.
enasp 2009-01-10
  • 打赏
  • 举报
回复
这好像都没有传入数据库吧!!

那读的时候怎么读啊
Chris_thanks 2009-01-10
  • 打赏
  • 举报
回复
可以在客户端做的事 就在客户端做 没必要传回服务端去
过河石头 2009-01-10
  • 打赏
  • 举报
回复

Regex reg = new Regex("(?<=[\\\\/]|^)[^\\\\/]*$", RegexOptions.IgnoreCase);
是多余的,可以不要
过河石头 2009-01-10
  • 打赏
  • 举报
回复
upFile.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upFile.aspx.cs" Inherits="Web.admin.imgLinks.upFile" %>

<!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 id="Head1" runat="server">
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" ForeColor="Red" Text="Label"></asp:Label>
<br />
<asp:FileUpload ID="fileImg" runat="server" Width="240px" />
<asp:Button ID="btSubmit" runat="server" Text="确定" CssClass="addBtn" OnClick="btSubmit_Click"
ValidationGroup="submit" />
</form>
</body>
</html>


upFile.aspx.cs
using System;
using System.Text.RegularExpressions;

namespace Web.admin.imgLinks
{
public partial class upFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btSubmit_Click(object sender, EventArgs e)
{

if (!fileImg.HasFile)
{
Label1.Text = "请选择上传图片/Flash!";
return;
}

string ftStr = "|.rar|.doc|.jpg|.bmp|";
if (ftStr.IndexOf("|" + System.IO.Path.GetExtension(fileImg.FileName).ToLower() + "|") < 0)
{
Label1.Text = "文件文件格式错误!";
return;
}

if (fileImg.PostedFile.ContentLength > 1024 * 1024)
{
Label1.Text = "文件超过1MB!";
return;
}

Regex reg = new Regex("(?<=[\\\\/]|^)[^\\\\/]*$", RegexOptions.IgnoreCase);
string saveFile = Server.MapPath(".") + "/" + System.IO.Path.GetFileName(fileImg.FileName);
fileImg.SaveAs(saveFile);



if (System.IO.File.Exists(saveFile))
{
Label1.Text = String.Format("上传成功! <a target='_blank' href='./{0}'>{0}</a>", System.IO.Path.GetFileName(fileImg.FileName));
}
else
{
Label1.Text= "上传失败!";
}
}
}
}
Jack2013tong 2009-01-10
  • 打赏
  • 举报
回复
这个不难,
private bool CheckPic()
{
bool isSafe = false;
HttpPostedFile pic = picfile.PostedFile;
//扩展名检查
string picext = System.IO.Path.GetExtension(picfile.PostedFile.FileName).ToLower();
if(picext == ".jpg" || picext==".gif" || picext == "bmp" || picext =="png")
{
isSafe = true;
}
else
{
isSafe = false;
Kit.Alert(this.Page,"图片格式不对,请转换成常见的图片格式 jpg,gif,bmp,png");
return isSafe;
}
//图片大小检查
if(pic.ContentLength > PicMaxLength())
{
isSafe = false;
Kit.Alert(this.Page,"超过图片限制大小");
return isSafe;
}
//图片尺寸检查
System.IO.Stream picstream = pic.InputStream;
System.Drawing.Image img = System.Drawing.Image.FromStream(picstream);
if(img.Width > 0 && img.Height >0)
{
isSafe = true;
//第三步验证身份证图片并上传
PicFileName = GetPicPath(picext);
PicSavePath = PicPath() + PicFileName;
SaveIdcard(PicSavePath);

picstream.Close();
picstream.Flush();
}
else
{
isSafe = false;
Kit.Alert(this.Page,"非法的图片文件");
//picstream.Close();
//picstream.Flush();
return isSafe;
}

return isSafe;
}
private string GetPicPath(string ext)
{
return DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+System.Guid.NewGuid().ToString().Substring(1,4)+ ext;
}
private void SaveIdcard(string idcard)
{
//string savepath = PicPath();
//savepath += idcard+".jpg";
picfile.PostedFile.SaveAs(idcard);
}
显示的部分自己加吧

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧