欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

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

介绍 评论 失效链接反馈

批量上传图片拖动上传
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

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复