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

发布时间:2020-11-08编辑:脚本学堂
本文介绍了asp.net fileupload控件上传图片,并提供预览图片功能的例子,asp.net fileupload 上传图片功能实例代码,需要的朋友参考下。

专题教程:asp.net fileupload控件上传文件教程大全

例子,asp.net fileupload控件上传图片并预览图片。
 

复制代码 代码示例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Managerment.aspx.cs" Inherits="Managerment" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Preview Image In FileUpload</title>
<%--CSS--%>
<style type="text/css">
 #newPreview{
 FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
</style>
</head>
<body>
<%--javascript--%>
<script language="javascript" type="text/javascript">
function PreviewImg(imgFile)
 {
var newPreview = document.getElementById("newPreview");
 newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
 newPreview.style.width = "80px";
 newPreview.style.height = "60px";
 }
</script>
<form id="form1" runat="server">
<div>
<%--添加onchange事件--%>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="PreviewImg(this)" />
</div>
<%--建立一个div来存放预览--%>
<div id="newPreview"></div>
</form>
</body>
</html>

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>

后端代码:
 

复制代码 代码示例:
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]
/>