欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET FileUpload 上传图片实例

程序员文章站 2024-03-08 22:37:04
复制代码 代码如下:
复制代码 代码如下:

<table style="width: 100%">
<tr>
<td>
<asp:validationsummary id="validationsummary1" runat="server" />
<br />
<asp:fileupload id="fileupload1" runat="server" />
 <asp:button id="btn_upload" runat="server" onclick="btn_upload_click"
text="upload" />
  
<asp:customvalidator id="customvalidator1" runat="server"
controltovalidate="fileupload1" display="static"
errormessage="you should only can upload image file such as files with .jpg or gif extension"
onservervalidate="image_validate">*</asp:customvalidator>
</td>
</tr>
</table>

add to code behind cs file
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
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;
using system.drawing;
public partial class practice_fileupload : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void btn_upload_click(object sender, eventargs e)
{
if (page.isvalid)
{
string path = @page.mappath("user_edit.aspx").replace("user_edit.aspx", "") + "documents\\";
string s = path + session["username"].tostring();
if (!system.io.directory.exists(path + session["username"].tostring()))
{
system.io.directory.createdirectory(path + session["username"].tostring());
}
if (fileupload1.hasfile)
{
fileupload1.saveas(server.mappath("~/seeker/documents/" + session["username"].tostring() + "/" + this.fileupload1.filename));
}
}
}
protected void image_validate(object source, servervalidateeventargs args)
{
string fileext = path.getextension(fileupload1.filename).tolower();
string filename = path.getfilename(fileupload1.filename);
if (fileext != ".jpg" && fileext != ".gif")
{
args.isvalid = false;
}
}
protected void customvalidator2_servervalidate(object source, servervalidateeventargs args)
{
bitmap bmip = new bitmap(fileupload1.postedfile.inputstream);
if (bmip.width > 100 | bmip.height > 100)
{
args.isvalid = false;
}
else
{
args.isvalid = true;
}
}
}

the default size of files uploaded by the fileupload control is 4mb. this solution was found from the internet。
值得注意的是,fileupload 默认上传文件最大为4mb。这是在网上找到的。
如果要增加,则可以在machine.config里面进行修改
复制代码 代码如下:

<httpruntime
executiontimeout = "110" [in seconds][number
maxrequestlength = "4096" [number]
requestlengthdiskthreshold = "80" [number]
usefullyqualifiedredirecturl = "false" [true|false]
minfreethreads = "8" [number]
minlocalrequestfreethreads = "4" [number]
apprequestqueuelimit = "5000" [number]
enablekerneloutputcache = "true" [true|false]
enableversionheader = "true" [true|false]
apartmentthreading = "false" [true|false]
requirerootedsaveaspath = "true" [true|false]
enable = "true" [true|false]
sendcachecontrolheader = "true" [true|false]
shutdowntimeout = "90" [in seconds][number]
delaynotificationtimeout = "5" [in seconds][number]
waitchangenotification = "0" [number]
maxwaitchangenotification = "0" [number]
enableheaderchecking = "true" [true|false]
/>