求FileUpload上传图片代码
发布网友
发布时间:2022-04-24 21:44
我来回答
共2个回答
热心网友
时间:2022-04-24 23:13
前台代码:VS2008 可以批量上传 自动命名 ---( 若上传文件名相同则不上传,并弹出提示框,若不同则提示上传成功,这儿就自己解决吧--可以遍历文件夹内的文件,如果是把文件名存到数据库的 就去匹配数据库的数据吧)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpDownFile.aspx.cs" Inherits="Management_UpDownFile" %>
<!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>文件上传</title>
<script src="../JavaScript/Upload.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div style="text-align: left; padding-left: 30px; padding-top: 10px;">
<p style="font-weight:bold;">文件上传:</p>
<p id="MyFile" style="width: 587px;">
<input type="file" size="25" name="File"/></p>
<p>
<input type="button" value="增加(Add)" onclick="addFile()" class="dddd" id="Button1"/>
<asp:Button runat="server" Text="开始上传" ID="UploadButton" OnClick="UploadButton_Click">
</asp:Button>
<asp:Button ID="Button_RES" runat="server" Text="关闭"
Width="70px" /></p>
<p>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="false" Font-Size="10pt"
Width="580px" BorderStyle="None" BorderColor="White"></asp:Label>
</p>
</div>
</form>
</body>
</html>
后台
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Management_UpDownFile : System.Web.UI.Page
{
Marry.BLL.SampleImage bllSampleImage = new Marry.BLL.SampleImage();
protected void Page_Load(object sender, EventArgs e)
{
//if (Session["UserID"] == null)
//{
// Response.Redirect("../General/Login.aspx");
// }
this.Button_RES.Attributes.Add("onclick", "javascript:if(confirm('确实要关闭当前页吗?')) window.close();");
}
private void SaveImages()
{
Random R = new Random();//创建产生随机数
/**/
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/**/
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("注:最大支持18M:" + "<br><br>");
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
/**/
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
/**/
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
///
if (fileExtension == ".jpg" || fileExtension == ".gif" || fileExtension == ".bmp" || fileExtension == ".png")
{
int val = 10 + R.Next(999);//产生随机数为99以内任意
int val1 = 10 + R.Next(999);//产生随机数为999以内任意
string FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + val.ToString() + val1.ToString() + fileExtension;
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/UplodeImage/") + FileName);
strMsg.Append("<FONT color=#ff0000>文件所在服务器位置:../UplodeImage/" + FileName + "</FONT><br><hr>");
}
else
{
int val = 10 + R.Next(999);//产生随机数为99以内任意
int val1 = 10 + R.Next(999);//产生随机数为999以内任意
string FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + val.ToString() + val1.ToString() + fileExtension;
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/UplodeWord/") + FileName);
strMsg.Append("<FONT color=#ff0000>文件所在服务器位置:../UplodeWord/" + FileName + "</FONT><br><hr>");
}
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
this.SaveImages();
}
}
热心网友
时间:2022-04-25 00:31
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class workerinforADD : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Label1.Text = "亲!你还没有选择图片!";
return;
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
string fileEX = filepath.Substring(filepath.LastIndexOf(".") + 1);
string serverpath = Server.MapPath("File'\'") + filename;
if (fileEX == "jpg" || fileEX == "bmp" || fileEX == "gif")
{
FileUpload1.PostedFile.SaveAs(serverpath);
Image1.ImageUrl = "File'\'" + filename;
Label1.Text = "上传成功!";
}
else
{
Label1.Text = "上传的图片扩展名错误";
}
}
}