教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c# FileUpload 上传图片的例子

c# FileUpload 上传图片的例子

发布时间:2015-11-17   编辑:jiaochengji.com
c# FileUpload 上传图片的例子,很简单,主要为了演示fileupload的用法,适合新手朋友参考。共二个文件,一个是aspx文件,一个是其后台代码。

c# FileUpload 上传图片的例子,很简单,主要为了演示fileupload的用法,适合新手朋友参考。
共二个文件,一个是aspx文件,一个是其后台代码。
1、aspx文件
<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>

2、后台代码
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;
}
}
}

注意:FileUpload 默认上传文件最大为4MB。可以在Machine.config中修改 maxRequestLength = "4096" 这个即可。
附:
1、<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]
/>
2、过滤说明后的内容:
<httpRuntime
executionTimeout = "110"
maxRequestLength = "4096"
requestLengthDiskThreshold = "80"
useFullyQualifiedRedirectUrl = "false"
minFreeThreads = "8"
minLocalRequestFreeThreads = "4"
appRequestQueueLimit = "5000"
enableKernelOutputCache = "true"
enableVersionHeader = "true"
apartmentThreading = "false"
requireRootedSaveAsPath = "true"
enable = "true"
sendCacheControlHeader = "true"
shutdownTimeout = "90"
delayNotificationTimeout = "5"
waitChangeNotification = "0"
maxWaitChangeNotification = "0"
enableHeaderChecking = "true"
/>

您可能感兴趣的文章:
.Net中FileUpload控件文件上传与验证实用例子
c# FileUpload 上传图片的例子
c#web控件FileUpload图片上传(并生成小图)
FileUpload控件客户端验证的例子
上传图片之前预览 fileupload
JS判断FileUpload的上传文件类型
实例讲解H5移动开发Ajax上传多张Base64格式图片到服务器
ajax 文件上传应用简单实现
在ASP.NET中存取图片到数据库的示例
php 多图片上传的简单例子(图文)

[关闭]
~ ~