ASP.NET FileUpload 上传图片实例

代码如下:

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

(0)

相关推荐

  • asp.net+FCKeditor上传图片显示叉叉图片无法显示的问题的解决方法

    弄了半天也没有找到原因,然后又重新到网上下载几个,还是不行,郁闷坏了,最后结合其他编辑器的用法,才知道是配置文件夹中上传文件夹的路径写错了 比如要上传到项目的根目录下的UserFiles下面,web.config的配置如下: 复制代码 代码如下: <appSettings> <add key="FCKeditor:BasePath" value="~/FCKeditor" /> <add key="FCKeditor:User

  • ASP.net WebAPI 上传图片实例

    复制代码 代码如下: [HttpPost] public Task<Hashtable> ImgUpload() {     // 检查是否是 multipart/form-data     if (!Request.Content.IsMimeMultipartContent("form-data"))         throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);     //文

  • asp.net MVC实现无组件上传图片实例介绍

    例子: 如我想上传一个图片到服务器端:asp页面 复制代码 代码如下: <form id="form1" runat="server" action="/bookIndex/fileUpLoad/(你准备处理的 ActionResult)" method="post" enctype="multipart/form-data"> <input type="file" i

  • ASP.NET下上传图片到数据库,并且读出图片的代码(详细版)

    首先在SQL Server中建立一个图片存储的数库表,ImageData Column为图象二进制数据储存字段,ImageContentType Column为图象文件类型记录字段,ImageDescription Column为储蓄图 象文件说明字段,ImageSize Column为储存图象文件长度字段,结构如下: 复制代码 代码如下: CREATE TABLE [dbo].[ImageStore] ( [ImageID] [int] IDENTITY (1, 1) NOT NULL , [

  • asp.net UpdatePanel实现无刷新上传图片

    1)前台 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o

  • asp.net上传图片保存到数据库的代码

    数据库:保存图片的数据格式 图象二进制数据储存字段前台: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadWork.aspx.cs" Inherits="meishuguan.UploadWork" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr

  • asp.net上传图片到服务器方法详解

    ASP.NET的FileUpload控件可用于上传文件到服务器.HoverTreeTop新增了一个"阅图"功能,图片就是用FileUpload上传的. 这里要说明的是上传图片限定文件名和文件大小等代码. 文件上传功能使用用户控件实现,在HoverTreePanel项目中的HTPanel\HControl\UCPictureAdd.ascx 控件, HoverTreeTop上传的图片文件暂时限定为jpg.png和gif.代码为: <asp:FileUpload runat=&quo

  • asp.net上传图片并作处理水印与缩略图的实例代码

    方法类: 复制代码 代码如下: upFileClass.cs using System;using System.Data;using System.Configuration;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.HtmlC

  • asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图

    如图: 点击浏览,选择图片之后,右面显示图片 第一步: 创建CtFileUpLoad.ascx 复制代码 代码如下: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="CtFileUpLoad.ascx.cs" Inherits="WebParts_CtFileUpLoad" %> <table cellpadding="0&quo

  • asp.net fileupload控件上传图片并预览图片

    本文为大家分享了fileupload控件实现上传图片后并进行预览图片的功能,并对web.config进行了配置,先看一下最终效果: 页面代码: <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1&quo

随机推荐