国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET FileUpload 上傳圖片實例

2024-07-10 13:19:25
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


<table>
<tr>
<td>
<asp:ValidationSummary runat="server" />
<br />
<asp:FileUpload runat="server" />
 <asp:Button runat="server"
Text="Upload" />
  
<asp:CustomValidator 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() + "http://www.survivalescaperooms.com/" + 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]
/>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石渠县| 申扎县| 马尔康县| 天峻县| 新闻| 革吉县| 嵊州市| 宜良县| 原阳县| 金昌市| 泾阳县| 色达县| 延安市| 云浮市| 亚东县| 饶河县| 楚雄市| 金乡县| 田东县| 凯里市| 深水埗区| 肥西县| 民权县| 城口县| 余庆县| 东城区| 项城市| 公主岭市| 平顺县| 资兴市| 古交市| 长春市| 陆丰市| 茂名市| 德清县| 炎陵县| 三河市| 石嘴山市| 无极县| 郴州市| 乐亭县|