当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET FileUpload 上传图片实例

ASP.NET FileUpload 上传图片实例

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<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]
/>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网