FileUpload 文件上传 多文件上传

aspx控件:FileUpload

<asp:FileUpload ID="FileUpload1" runat="server" />

如果要一次上传多个文件 控件属性设置:AllowMultiple="True"

代码:

如果.NET Framework为4.5及以上的  可以使用“GetMultiple”方法

HttpFileCollection hc = HttpContext.Current.Request.Files;
var files = hc.GetMultiple("FileUpload1");
foreach (HttpPostedFile item in files)
{

}

如果是4.5以下的

HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
if (hfc.Count > 0)
{
    if (hfc[0].ContentLength < 1000000)
    {
        string ImgExtend = getExtend(hfc[0].FileName);
        if (ImgExtend == ".bmp" || ImgExtend == ".jpg" || ImgExtend == ".gif" || ImgExtend == ".png")
        {
                                string path = "UpFiles\\WebPageImage\\" + DateTime.Now.ToString("yyyyMMdd") + "\\" + FromUserName;
                                if (Directory.Exists(System.Web.HttpContext.Current.Server.MapPath("/") + path) == false)
                                {
                                    Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath("/") + path);
                                }

                                content = DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + getExtend(hfc[0].FileName);

                                savepath = System.Web.HttpContext.Current.Server.MapPath("/") + path + "\\" + content;
                                file = "\\" + path + "\\" + content;
                                Session["ImgPath"] = file;
                                hfc[0].SaveAs(savepath);
                                str = "上传成功";
                            }
                            else if (ImgExtend == "")
                            { }
                            else
                            {
                                str = "上传的图片格式只能是jpg、png!";
                            }
                        }
                        else
                        { str = "上传的图片大小为1M以下!"; }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
commons-fileupload是一个用于处理文件的Java类库。它提供了简单易用的API,可以用于实现文件功能。要使用commons-fileupload,你需要进行以下步骤: 1. 导入commons-fileupload库:下载commons-fileupload库的jar文件,并将其添加到你的项目中。 2. 创建Servlet或Controller类:创建一个处理文件的Servlet或Controller类。 3. 创建DiskFileItemFactory对象:使用DiskFileItemFactory类创建一个文件项工厂对象,设置临时文件存储路径和内存缓冲区大小。 4. 创建ServletFileUpload对象:使用ServletFileUpload类创建一个文件对象,并设置文件的大小限制和字符编码。 5. 解析请求:使用ServletFileUpload对象的parseRequest()方法解析请求,得到FileItem对象的列表。 6. 处理文件:遍历FileItem对象的列表,对每个文件项进行处理。你可以判断文件项是否是普通字段还是文件,以及获取字段名称、文件名、文件大小等信息。 以下是一个简单的示例代码: ```java import org.apache.commons.fileupload.*; import org.apache.commons.fileupload.disk.*; import org.apache.commons.fileupload.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class FileUploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) { // 处理普通字段 String fieldName = item.getFieldName(); String fieldValue = item.getString(); // ... } else { // 处理文件 String fieldName = item.getFieldName(); String fileName = item.getName(); long fileSize = item.getSize(); // ... } } // 文件成功 response.getWriter().println("文件成功!"); } catch (FileUploadException e) { // 文件失败 response.getWriter().println("文件失败!"); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值