ASP.NET FileUpload 上傳圖片實(shí)例
<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。
值得注意的是,F(xiàn)ileUpload 默認(rèn)上傳文件最大為4MB。這是在網(wǎng)上找到的。
如果要增加,則可以在Machine.config里面進(jìn)行修改
<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控件上傳圖片并預(yù)覽圖片
- ASP.net WebAPI 上傳圖片實(shí)例
- asp.net上傳圖片并作處理水印與縮略圖的實(shí)例代碼
- asp.net MVC實(shí)現(xiàn)無組件上傳圖片實(shí)例介紹
- asp.net+FCKeditor上傳圖片顯示叉叉圖片無法顯示的問題的解決方法
- ASP.NET下上傳圖片到數(shù)據(jù)庫,并且讀出圖片的代碼(詳細(xì)版)
- asp.net上傳圖片保存到數(shù)據(jù)庫的代碼
- asp.net UpdatePanel實(shí)現(xiàn)無刷新上傳圖片
- asp.net 自定義控件實(shí)現(xiàn)無刷新上傳圖片,立即顯示縮略圖,保存圖片縮略圖
- asp.net上傳圖片到服務(wù)器方法詳解
相關(guān)文章
.NET程序集引用COM組件MSScriptControl遇到問題的解決方法
這篇文章主要為大家詳細(xì)介紹了.NET程序集引用COM組件MSScriptControl遇到問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01ASP.NET?Core框架探索之Authentication的權(quán)限認(rèn)證過程解析
這篇文章主要介紹了ASP.NET?Core框架探索之Authentication的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03ASP.NET?MVC5實(shí)現(xiàn)文件上傳與地址變化處理(5)
這篇文章主要介紹了ASP.NET?MVC5實(shí)現(xiàn)文件上傳與地址變化處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09ASP.NET技巧:做個(gè)DataList可分頁的數(shù)據(jù)源
ASP.NET技巧:做個(gè)DataList可分頁的數(shù)據(jù)源...2006-09-09asp.net core3.1 引用的元包dll版本兼容性問題解決方案
這篇文章主要介紹了asp.net core 3.1 引用的元包dll版本兼容性問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03ASP.NET 網(wǎng)站開發(fā)中常用到的廣告效果代碼
在ASP.NET項(xiàng)目開發(fā)中,會(huì)被要求添加廣告,有翻屏效果、有廣告輪流顯示、飄浮廣告、左側(cè)廣告、右側(cè)廣告等。2010-04-04asp.net c# 調(diào)用百度pai實(shí)現(xiàn)在線翻譯,英文轉(zhuǎn)中文
本文詳細(xì)介紹asp.net c# 調(diào)用百度pai 實(shí)現(xiàn)在線翻譯以及英文轉(zhuǎn)中文實(shí)現(xiàn)代碼,需要了解的朋友可以參考下2012-12-12asp.net網(wǎng)站開發(fā)包wq.dll打包下載
這個(gè)wq.dll主要是用來給Web群和C#聯(lián)盟群及GUI群的朋友使用的,其它群和使用控件開發(fā)web的朋友可以直接無視,這個(gè)封裝好的包是一個(gè)基礎(chǔ)開發(fā)包,可以輕松的幫你完成一些小型網(wǎng)站的開發(fā),支持.Net Framework2.0(及以上平臺(tái))。2009-10-10js獲取Treeview選中的節(jié)點(diǎn)(C#選中CheckBox項(xiàng))
方法網(wǎng)上有很多,試了一下都有瑕疵,于是設(shè)置斷點(diǎn)調(diào)試,各個(gè)屬性查找有用的字段,終于找到,接下來與大家分享解決方法,需要了解的朋友可以參考下2012-12-12