Esempio n. 1
0
        /// <summary>
        /// 断点续传检测、MD5检测
        /// </summary>
        /// <returns></returns>
        public UploaderResult ProcessCheck(HttpRequestBase request, string saveFileName, string savepath = null
                                           , Func <HttpPostedFileBase, string> setfilename = null, Func <string, bool> md5check = null)
        {
            long o = _webUploaderField.Size % _webUploaderField.Chunksize;



            int chunks = _webUploaderField.Chunksize != 0 ?
                         Convert.ToInt32(_webUploaderField.Size / _webUploaderField.Chunksize) : 1;

            if (o != 0)
            {
                chunks = chunks + 1;
            }
            UploaderResult obj = new UploaderResult();

            if (md5check(_webUploaderField.Md5))
            {
                obj.Filish     = true;
                obj.FileExtist = true;//表明文件已存在于服务器
                obj.Message    = "文件已存在于服务器、跳过上传";
                return(obj);
            }
            WebUploader.allChunks[_webUploaderField.FileSavePath] = new List <int>();
            int j = 0;

            for (int i = 0; i < chunks; i++)
            {
                if (File.Exists(_webUploaderField.FileSaveChunkPath + i.ToString()))
                {
                    obj.ChunkNum.Add(i);//服务器已经存在的区块编号
                    WebUploader.allChunks[_webUploaderField.FileSavePath].Add(i);
                    j++;
                }
            }
            obj.Filish  = false;
            obj.Message = string.Format("服务器已经存在区块数量{0},总区块数量{1},占比{2}%", j
                                        , _webUploaderField.Chunks, _webUploaderField.Chunks != 0 && j != 0
                ? Convert.ToDouble(Convert.ToDouble(j) / Convert.ToDouble(_webUploaderField.Chunks)) * 100 : 0);
            return(obj);
        }
Esempio n. 2
0
        /// <summary>
        /// 上传分片文件
        /// </summary>
        /// <param name="request"></param>
        /// <param name="saveFileName"></param>
        /// <param name="savepath"></param>
        /// <param name="setfilename"></param>
        /// <param name="md5check"></param>
        /// <returns></returns>
        public UploaderResult Process(HttpRequestBase request, string saveFileName, Func <string, bool> dataIn, string savepath = null
                                      , Func <HttpPostedFileBase, string> setfilename = null, Func <string, bool> md5check = null)
        {
            InitWebUploaderField(request, savepath, saveFileName);

            #region 跳转到分片检测
            if (request[chunkFlag] != null && Convert.ToBoolean(request[chunkFlag]))
            {
                return(ProcessCheck(request, saveFileName, savepath, setfilename, md5check));
            }
            #endregion

            UploaderResult obj = new UploaderResult();

            _webUploaderField.File = request.Files.Count == 0 ? null : request.Files[0];

            _webUploaderField.Filename = _webUploaderField.File == null ? "" : _webUploaderField.File.FileName;

            if (_webUploaderField.File == null)
            {
                obj.Message = "上传文件为空";
                return(obj);
            }
            //创建文件夹(查看路径是否存在)
            if (!Directory.Exists(_webUploaderField.Path))
            {
                Directory.CreateDirectory(_webUploaderField.Path);
            }
            if (_webUploaderField.Chunks != 0)
            {
                WebUploader.allChunks[_webUploaderField.FileSavePath].Add(_webUploaderField.Chunk);
                //_webUploaderField.File.SaveAs(_webUploaderField.FileSaveChunkPath + _webUploaderField.Chunk)
                try
                {
                    Stream stream = _webUploaderField.File.InputStream;
                    byte[] b      = new byte[stream.Length];//新建一个字节数组
                    stream.Read(b, 0, b.Length);
                    // 设置当前流的位置为流的开始
                    stream.Seek(0, SeekOrigin.Begin);
                    using (FileStream fstream = new FileStream(_webUploaderField.FileSaveChunkPath + _webUploaderField.Chunk, FileMode.Create, FileAccess.Write))
                    {
                        fstream.Write(b, 0, b.Length);
                    }
                }
                catch
                {
                }
                obj.Filish  = false;
                obj.Message = string.Format("当前上传区块{0},总区块:{1},模式:分片上传", _webUploaderField.Chunk, _webUploaderField.Chunks);
                if (WebUploader.allChunks[_webUploaderField.FileSavePath].Count == _webUploaderField.Chunks)
                {
                    var file = _webUploaderField.FileSavePath;
                    //var currentSavePath = file.Substring(0,file.LastIndexOf('\\'));
                    //var fileSuffix = file.Substring(file.LastIndexOf("."), file.Length - file.LastIndexOf("."));
                    //var sFileName = file.Substring(file.LastIndexOf('\\')+1, file.LastIndexOf('.')-file.LastIndexOf('\\')-1);
                    ////saveFileName = saveFileName + fileSuffix;
                    //saveFileName = (currentSavePath +"\\"+ sFileName + fileSuffix);

                    using (FileStream fsw = new FileStream(NewFileName(file), FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        BinaryWriter bw = new BinaryWriter(fsw);
                        for (int j = 0; j < _webUploaderField.Chunks; j++)
                        {
                            if (File.Exists(_webUploaderField.FileSaveChunkPath + j))
                            {
                                bw.Write(File.ReadAllBytes(_webUploaderField.FileSaveChunkPath + j));
                                bw.Flush();
                                File.Delete(_webUploaderField.FileSaveChunkPath + j);
                            }
                            else
                            {
                            }
                        }
                        //bw.Flush();
                    }
                    List <int> filekeyvalue = new List <int>();
                    obj.Name = _webUploaderField.Filename;
                    WebUploader.allChunks.TryRemove(_webUploaderField.FileSavePath, out filekeyvalue);
                    dataIn(_webUploaderField.FileSavePath);
                    obj.Filish  = true;
                    obj.Message = string.Format("上传已全部完成");
                }
            }
            else
            {
                _webUploaderField.File.SaveAs(_webUploaderField.FileSavePath);
                dataIn(_webUploaderField.FileSavePath);
                obj.Filish  = true;
                obj.Name    = _webUploaderField.Filename; //文件最终保存名称
                obj.Message = "上传已完成、模式 no chunk";
            }
            return(obj);
        }