ASP.NET(C#)实现一次性上传多张图片(多个文件)

ASP.NET 多张图片上传,并可以预览。 一、首先先上效果图: 1、上传前 2、上传后 二、主要使用控件:FileUpload,DataList展示多张图片 三、代码 1、前台aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductZiZhiUpload.aspx.cs" Inherits="ProductZiZhiUpload" %... 阅读详情
ASP.NET(C#)实现一次性上传多张图片(多个文件)
HTML文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUp.aspx.cs" Inherits="FileUp" %>
<!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>ASP.NET(C#)实现一次性上传多张图片(多个文件)</title>
<script type="text/javascript">
var i=1;
function addFile()
{
    
if (i<8)
    
{
        
var str = '<BR> <input type="file" name="File" runat="server" style="width: 200px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" />';
        document.getElementById(
'MyFile').insertAdjacentHTML("beforeEnd",str);
    }

    
else
    
{
        alert(
"您一次最多只能上传8张图片!");
    }

    i
++;
}

</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" align="center" border="0" cellpadding="1" cellspacing="1" class="table">
    
<tr>
        
<td align="center"><font color="#0000ff" face="宋体" size="3"><strong>上传图片</strong></font></td>
    
</tr>
    
<tr>
        
<td align="center" style="width: 734px"></td>
    
</tr>
    
<tr>
        
<td align="center" style="width: 734px">
        
<asp:Panel ID="Panel5" runat="server">
        
<table width="100%">
            
<tr>
                
<td align="right" style="width: 100px"></td>
                
<td align="left" style="width: 500px">说明:点增加图片按钮可一次上传多张图片,可为每张图片写上一句不超过20个字的描述。单张图片大小不大于1024k</td>
            
</tr>
            
<tr>
                
<td align="right" style="width: 100px">请选择图片:<br /></td>
                
<td align="left" style="width: 500px"><p id="MyFile"><input onclick="addFile()" type="button" value="增加图片(Add)"></p><br /><input id="File1" type="file" name="File" runat="server" style="width: 245px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" /></td>
            
</tr>
            
<tr>
                
<td align="right" style="width: 100px"></td>
                
<td align="left" style="width: 500px"><asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click"  /></td>
            
</tr>
            
<tr>
                
<td align="right" style="width: 100px"></td>
                
<td align="left" style="width: 500px"><asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td>
            
</tr>
        
</table>
        
</asp:Panel>
        
</td>
    
</tr>
    
<tr>
        
<td align="center" style="width: 734px"></td>
    
</tr>
</table>
</div>
</form>
</body>
</html>
cs文件:
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;

public partial class FileUp : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
    }

    
protected void btnUpload_Click(object sender, EventArgs e)
    
{
        lblMessage.Text 
= "";
        lblMessage.Visible 
= false;
        System.Web.HttpFileCollection files 
= System.Web.HttpContext.Current.Request.Files;
        System.Text.StringBuilder strmsg 
= new System.Text.StringBuilder("");
        
string[] rd = Request.Form[1].Split(',');//获得图片描述的文本框字符串数组,为对应的图片的描述
        
//string albumid=ddlAlbum.SelectedValue.Trim();
        int ifile;
        
for (ifile = 0; ifile < files.Count; ifile++)
        
{
            
if (files[ifile].FileName.Length > 0)
            
{
                System.Web.HttpPostedFile postedfile 
= files[ifile];
                
if (postedfile.ContentLength / 1024 > 1024)//单个文件不能大于1024k
                {
                    strmsg.Append(Path.GetFileName(postedfile.FileName) 
+ "---不能大于1024k<br>");
                    
break;
                }

                
string fex = Path.GetExtension(postedfile.FileName);
                
if (fex != ".jpg" && fex != ".JPG" && fex != ".gif" && fex != ".GIF")
                
{
                    strmsg.Append(Path.GetFileName(postedfile.FileName) 
+ "---图片格式不对,只能是jpg或gif<br>");
                    
break;
                }

            }

        }

        
if (strmsg.Length <= 0)    //说明图片大小和格式都没问题
        {
            
//以下为创建图库目录
            string dirpath = Server.MapPath("51aspx");

            
if (Directory.Exists(dirpath) == false)
            
{
                Directory.CreateDirectory(dirpath);
            }

            Random ro 
= new Random();
            
int name = 1;
            
for (int i = 0; i < files.Count; i++)
            
{
                System.Web.HttpPostedFile myFile 
= files[i];
                
string FileName = "";
                
string FileExtention = "";
                FileName 
= System.IO.Path.GetFileName(myFile.FileName);
                
string stro = ro.Next(100, 100000000).ToString() + name.ToString();//产生一个随机数用于新命名的图片
                string NewName = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + stro;
                
if (FileName.Length > 0)//有文件才执行上传操作再保存到数据库
                {
                    FileExtention 
= System.IO.Path.GetExtension(myFile.FileName);
                    
string ppath = dirpath + @"" + NewName + FileExtention;
                    myFile.SaveAs(ppath);
                }

                name 
= name + 1;//用来重命名规则的变量
            }

            Response.Write(
"<script>alert('恭喜,图片上传成功!')</script>");
        }

        
else
        
{
            lblMessage.Text 
= strmsg.ToString();
            lblMessage.Visible 
= true;
        }

    }

}
c# Asp.Net上传图片 c# Asp.Net上传图片 aspx页面 <asp:Image ID="imgBindShow" runat="server" /> <asp:FileUpload ID="uploadImage" runat="server" accept=".jpg,.png,.jpeg"/> <as... 阅读详情

相关推荐

C#和ASP.NET实现的批量上传图片项目源码解析

在ASP.NET应用程序中,可以通过路由配置定义自定义路由规则。上述代码示例定义了一个新的路由规则,将所有指向的请求映射到Customer控制器的Add动作方法。ImageMagick是一款功能强大的跨平台图片处理软件,支持超过200种图片格式的读取和转换。它包含命令行工具、动态库和开发工具包(SDK),适合在服务器端进行高效的图片处理。ImageMagick可以通过其丰富的API实现各种图片处理任务,如格式转换、图片压缩、裁剪、旋转、缩放等,是后端服务中理想的图片处理工具。

weixin_36427956的博客 1091

ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)

原文出处:http://www.jb51.net/article/28538.htm 我们就要让这些文件上传框动态添加,下面我以我做的一个图库管理中的上传图片的功能为例 在做asp.net的Web开发的时候,我们经常会遇到一次性上传多个文件的需求。通常我们的解决方法是固定放多个上传文件框,这样的解决办法显然是不合理的,因为一次上传多个,就意味着数量不确定。因此我们就要让这些文件上

chelen_jak的专栏 1万+

C#实现ASP.NET上传视频自动截取首帧图片

NET框架和C#语言是微软公司推出的优秀开发工具和编程语言。它们被广泛应用于构建各种类型的应用程序,从桌面应用到企业级的Web服务,无所不包。.NET框架提供了一个高度一致的编程环境,C#作为该框架的首选语言,提供了面向对象、类型安全等特性,使得开发者可以快速地编写高效、可靠的应用程序。.NET框架是一种多语言编程环境,它为开发者提供了一系列的库、工具和服务,用于构建和运行Windows应用程序。它包含一个庞大的类库,覆盖了从文件访问、数据操作到网络通信等多个方面,极大地简化了软件开发流程。

weixin_29323977的博客 1237

ASP.NET(C#)实现一次性动态上传多张图片(多个文件)

在做asp.net的Web开发的时候,我们经常会遇到一次性上传多个文件的需求。通常我们的解决方法是固定放多个上传文件框,这样的解决办法显然是不合理的,因为一次上传多个,就意味着数量不确定。因此我们就要让这些文件上传框动态添加,下面我以我做的一个图库管理中的上传图片的功能为例先看效果:打开的初始界面:默认是上传一个图片,但当我们点“增加图片”按钮时可以实现选择多个图片及其描述同时上传,本功能限制一...

weixin_34242658的博客 181

两种方法解决这个问题OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.

两种方法解决这个问题OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.在进行深度学习比如在训练yolov8模型时,在加载训练集和验证集时会报以下那个错误时会导致模型训练中断,有以下两个方法去解决这个问题,来使模型正常训练。

weixin_73044854的博客 2万+

NAO V6 开发环境的配置(附所有文件)

NAO V6开发环境配置前言环境Robot SettingsChoregraphe 2.8.5.10python2.7+naoqi2.8+opencv4.1.0putty与FileZilla 前言 学校的机器人摸索了一段时间,之前摸爬滚打探索环境,之前没接触这类机器人可能会非常艰辛,希望能给需要的人一些帮助。Nao与pepper通用 环境 win10(x64)+naoqi2.8 1、Robot S...

wbhhello的博客 3225
上一篇: url传递中文的解决方案总结
下一篇: 互联网创业的10个建议
useruse
博客等级 码龄19年 11粉丝 91原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值