在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 批量上传图片和拖动上传,分块上传

批量上传图片和拖动上传,分块上传

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:2.20M
  • 下载次数:80
  • 浏览次数:748
  • 发布时间:2018-08-16
  • 实例类别:C#语言基础
  • 发 布 人:MissCJ
  • 文件格式:.rar
  • 所需积分:1

实例介绍

【实例简介】批量上传图片和拖动上传
【实例截图】from clipboard

from clipboardfrom clipboard

from clipboard

from clipboard

【核心代码】

<%@ WebHandler Language="C#" Class="GenericHandler1" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;


using System.Text;
using System.IO;

public class GenericHandler1 : IHttpHandler,IRequiresSessionState{
    string ab = string.Empty;
    string getcookiename = string.Empty;
    public void ProcessRequest(HttpContext context)
    {

        context.Response.ContentType = "text/plain";
        context.Response.Write("123");
        getcookiename = "1.txt";//context.Request.Cookies["Txt"].Value.ToString();
        if (HttpContext.Current.Session["a"] == null)
        {
            HttpContext.Current.Session.Add("a", "");
            ab = HttpContext.Current.Session["a"].ToString();
        }
        //context.Response.Write(HttpContext.Current.Session["1"].ToString());
        else
        {
            ab = HttpContext.Current.Session["a"].ToString();
        }

        UploadFile(context);
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void UploadFile(HttpContext context)
    {
        context.Response.CacheControl = "no-cache";
        string s_rpath = FileHelper.GetUploadPath();//@"E:\My Documents\Visual Studio 2008\WebSites\SWFUpload\demos\applicationdemo.net";

        byte[] byData = new byte[1000];
        char[] charData = new char[1000];
        string Datedir = DateTime.Now.ToString("yy-MM-dd");
        string updir = s_rpath "\\" Datedir;
        string ulrname1= @"C:";
        // s_rpath "\\" Datedir;
        string extname = string.Empty;
        string fullname = string.Empty;
        string filename = string.Empty;
        string Name12 = string.Empty;
        string sessionname = string.Empty;
        string asv = string.Empty;
        if (context.Request.Files.Count > 0)
        {
            try
            {

                for (int j = 0; j < context.Request.Files.Count; j )
                {

                    HttpPostedFile uploadFile = context.Request.Files[j];
                    int offset = Convert.ToInt32(context.Request["chunk"]);
                    int total = Convert.ToInt32(context.Request["chunks"]);
                    string name = context.Request["name"];
                    //文件没有分块
                    /*      if (total == 1)
                         { */

                    if (uploadFile.ContentLength > 0)
                    {
                        if (!Directory.Exists(updir))
                        {
                            Directory.CreateDirectory(updir);
                        }
                        extname = Path.GetExtension(uploadFile.FileName);
                        fullname = DateTime.Now.Year.ToString() DateTime.Now.Month.ToString() DateTime.Now.Day.ToString() DateTime.Now.Hour.ToString() DateTime.Now.Minute.ToString() DateTime.Now.Second.ToString();
                        filename = uploadFile.FileName;
                        Name12 = filename.Substring(filename.Length-4,4);

                        Random ran=new Random();
                        int RandKey=ran.Next(1,1000);
                        uploadFile.SaveAs(string.Format("{0}\\{1}", ulrname1, "GoodsCoverImage" RandKey.ToString() fullname Name12));
                        sessionname= "GoodsCoverImage" RandKey.ToString() fullname Name12 ";";
                        ab = ab sessionname;
                    }

                    /*}
                    else
                    { */
                    //文件 分成多块上传
                    fullname = WriteTempFile(uploadFile, offset);
                    if (total - offset == 1)
                    {
                        //如果是最后一个分块文件 ,则把文件从临时文件夹中移到上传文件 夹中
                        System.IO.FileInfo fi = new System.IO.FileInfo(fullname);
                        string oldFullName = string.Format("{0}\\{1}", updir, uploadFile.FileName ".part");
                        FileInfo oldFi = new FileInfo(oldFullName);
                        if (oldFi.Exists)
                        {
                            //文件名存在则删除旧文件 
                            oldFi.Delete();
                        }
                        fi.MoveTo(oldFullName);
                    }
                    /*    } */
                }

                //    StreamWriter sw = new StreamWriter(ulrname1 "\\test.txt");


                StreamWriter sw =File.AppendText(ulrname1 "\\" getcookiename.Trim());
                sw.Write(ab);
                sw.Close();


                HttpContext.Current.Session.Add("abc","1233");
            }

            catch (Exception ex)
            {
                context.Response.Write("Message" ex.ToString());
            }



        }
    }
    /// <summary>
    /// 保存临时文件 
    /// </summary>
    /// <param name="uploadFile"></param>
    /// <param name="chunk"></param>
    /// <returns></returns>
    private string WriteTempFile(HttpPostedFile uploadFile, int chunk)
    {

        string tempDir = FileHelper.GetTempPath();
        if (!Directory.Exists(tempDir))
        {
            Directory.CreateDirectory(tempDir);
        }
        string fullName = string.Format("{0}\\{1}", tempDir, uploadFile.FileName);
        if (chunk==0)
        {
            //如果是第一个分块,则直接保存
            uploadFile.SaveAs(fullName);
        }
        else
        {
            //如果是其他分块文件 ,则原来的分块文件,读取流,然后文件最后写入相应的字节
            FileStream fs = new FileStream(fullName, FileMode.Append);
            if (uploadFile.ContentLength>0)
            {
                int       FileLen = uploadFile.ContentLength;
                byte[] input = new byte[FileLen];

                // Initialize the stream.
                System.IO.Stream MyStream = uploadFile.InputStream;

                // Read the file into the byte array.
                MyStream.Read(input, 0, FileLen);

                fs.Write(input,0,FileLen);
                fs.Close();
            }
        }


        return fullName;
    }

}

/// <summary>
///FileHelper 的摘要说明
/// </summary>
public class FileHelper
{
    public FileHelper()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    /// <summary>
    /// 获取上传目录
    /// </summary>
    /// <returns></returns>
    public static string GetUploadPath()
    {
        string path = HttpContext.Current.Server.MapPath("~/");
        string dirname = GetTempDirName();//GetDirName();
        string uploadDir = path "\\" dirname;
        CreateDir(uploadDir);
        return uploadDir;
    }
    /// <summary>
    /// 获取临时目录
    /// </summary>
    /// <returns></returns>
    public static string GetTempPath()
    {
        string path = HttpContext.Current.Server.MapPath("~/");
        string dirname = GetTempDirName();
        string uploadDir = path "\\" dirname;
        CreateDir(uploadDir);
        return uploadDir;
    }
    private static string GetDirName()
    {
        return System.Configuration.ConfigurationManager.AppSettings["uploaddir"];
    }
    private static string GetTempDirName()
    {
        return System.Configuration.ConfigurationManager.AppSettings["tempdir"];
    }
    public static void CreateDir(string path)
    {
        if (!System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
        }
    }
}


from clipboard


实例下载地址

批量上传图片和拖动上传,分块上传

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 12322 发表于: 2019-12-06 12:02 29
不是源码,发布好后的文件

支持(0) 盖楼(回复)

第 2 楼 12322 发表于: 2019-12-06 12:43 17
不是源码,发布好后的文件

12322 2019-12-06 12:02 29

运行不了 主要文件丢失

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有2条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警