ASP.NET FileUpload 上传图片实例
2018-10-12 14:00
741 查看
<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]
/>
您可能感兴趣的文章:
相关文章推荐
- ASP.NET FileUpload 上传图片实例
- ASP.NET FileUpload 上传图片实例
- VS2005图片上传源码 ASP.NET 图片上传源码 asp:FileUpload 图片上传源码
- 分享用Asp.net做的几个实例,数据报表、二维码、数据抓取、批量上传图片
- asp.net MVC实现无组件上传图片实例介绍
- asp.net FileUpload 判断上传文件是否为图片
- asp.net批量上传图片的实例
- asp.net图片上传-简单实用(VS2008 FileUpload控件+ASP.NET2.0)按时间+三位随机数重命名
- asp.net C#如何上传指定路径的图片,不用FileUpload控件[转]
- asp.net在ie7中使用FileUpload上传前预览图片
- 用FileUpload控件上传图片 (asp.net C#)
- asp.net在ie7中使用FileUpload上传前预览图片
- ASP.NET 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字水印和图片的水印图
- asp.net图片上传-简单实用(VS2008 FileUpload控件+ASP.NET2.0)按时间+三位随机数重命名
- asp.net在ie7中使用FileUpload上传前预览图片
- ASP.net WebAPI 上传图片实例
- ASP.NET使用FileUpLoad上传图片
- ASP.NET中FileUpload文件上传控件应用实例
- Asp.Net的FileUpload类实现上传文件实例
- Asp.Net的FileUpload类实现上传文件实例