
{
Boolean fileOK = false
String path = Server.MapPath("扒迅~/upload/")
if (FileUpload1.HasFile)
{
String fileExtension =System.IO.Path.GetExtension(FileUpload1.FileName).ToLower()
String[] allowedExtensions = { ".gif", ".jpeg", ".jpg" }
for (int i = 0i <allowedExtensions.Lengthi++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true
}
}
}
Label1.Text = fileOK.ToString()
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path
+ FileUpload1.FileName)
Label1.Text = "上冲此芦传散带成功"
}
catch (Exception ex)
{
Label1.Text = "上传失败"
}
}
else
{
Label1.Text = "类型不正确"
}
}
将文件放入工程根目录下的upload中
//从web.config读取文件上传路径 string strFileUploadPath = ConfigurationManager.AppSettings["FileUplodePath"].ToLower()//从列表框中读取选择的文件前台代码:VS2008 可以批量上传 自动命名 ---( 若上传文件名相同则不上传,并d出提示框,若不同则提示上传成功,这儿就自己解决吧--可以遍历文件夹内的文件,如果是把文件名存到数据库的 就去匹配数据库的数据吧)<%@ 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: leftpadding-left: 30pxpadding-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 = 0iFile <files.CountiFile++)
{
/**/
///'检查文件扩展名字
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()
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)