コード例 #1
0
        /// <summary>
        /// 一次性上传
        /// </summary>
        /// <param name="uploadNode"></param>
        /// <returns></returns>
        public static UploadResult Process(UploadNode uploadNode)
        {
            try
            {
                string ex        = Path.GetExtension(uploadNode.file.FileName);
                string localName = Guid.NewGuid().ToString("N") + ex;
                if (!Directory.Exists(uploadNode.localPath))
                {
                    Directory.CreateDirectory(uploadNode.localPath);
                }
                uploadNode.file.SaveAs(Path.Combine(uploadNode.localPath, localName));

                return(new UploadResult()
                {
                    Status = true, LocalName = localName
                });
            }
            catch (Exception ex)
            {
                return(new UploadResult()
                {
                    Status = false, Error = ex.ToString()
                });
            }
        }
コード例 #2
0
        /// <summary>
        /// 分片合并,分片文件上传2
        /// </summary>
        /// <param name="uploadNode"></param>
        /// <returns></returns>
        public static UploadResult MergeSplitFiles(UploadNode uploadNode)
        {//uploadNode.guid + uploadNode.id =唯一
            try
            {
                string sourcePath = Path.Combine(uploadNode.localPath, uploadNode.guid + uploadNode.id + "/"); //源数据文件夹
                var    fileGuid   = Guid.NewGuid().ToString("N");
                string targetPath = Path.Combine(uploadNode.localPath, fileGuid + "." + uploadNode.fileExt);   //合并后的文件

                DirectoryInfo dicInfo = new DirectoryInfo(sourcePath);
                if (Directory.Exists(Path.GetDirectoryName(sourcePath)))
                {
                    FileInfo[] files = dicInfo.GetFiles();
                    foreach (FileInfo file in files.OrderBy(f => int.Parse(f.Name)))
                    {
                        FileStream   addFile   = new FileStream(targetPath, FileMode.Append, FileAccess.Write);
                        BinaryWriter AddWriter = new BinaryWriter(addFile);

                        //获得上传的分片数据流
                        Stream       stream     = file.Open(FileMode.Open);
                        BinaryReader TempReader = new BinaryReader(stream);
                        //将上传的分片追加到临时文件末尾
                        AddWriter.Write(TempReader.ReadBytes((int)stream.Length));
                        //关闭BinaryReader文件阅读器
                        TempReader.Close();
                        stream.Close();
                        AddWriter.Close();
                        addFile.Close();
                    }
                    DeleteFolder(sourcePath);

                    string filePathName = string.Empty;

                    filePathName = fileGuid + uploadNode.fileExt;

                    return(new UploadResult()
                    {
                        Status = true, LocalName = filePathName
                    });
                }
                else
                {
                    return(new UploadResult()
                    {
                        Status = false, Error = "(被分片文件)文件夹丢失"
                    });
                }
            }
            catch (Exception ex)
            {
                return(new UploadResult()
                {
                    Status = false, Error = ex.ToString()
                });
            }
        }
コード例 #3
0
        public void Upload()
        {
            var uploadNode = new UploadNode();

            uploadNode.chunked = Request["chunks"] == null ? false : true;
            uploadNode.guid    = Request["guid"] == null ? null : Request["guid"];       //自定义
            uploadNode.fileExt = Request["fileExt"] == null ? null : Request["fileExt"]; //自定义,合并专用
            uploadNode.chunks  = Request["chunks"] == null ? 0 : int.Parse(Request["chunks"]);
            uploadNode.chunk   = Request["chunk"] == null ? 0 : int.Parse(Request["chunk"]);


            uploadNode.id               = Request["id"] == null ? null : Request["id"];
            uploadNode.name             = Request["name"] == null ? null : Request["name"];
            uploadNode.type             = Request["type"] == null ? null : Request["type"];
            uploadNode.lastModifiedDate = Request["lastModifiedDate"] == null ? null : Request["lastModifiedDate"];
            uploadNode.size             = Request["size"] == null ? 0 : int.Parse(Request["size"]);
            uploadNode.file             = Request.Files.Count == 0 ? null : Request.Files[0];
            string saveFolder = Request["saveFolder"] == null ? null : Request["saveFolder"];

            uploadNode.localPath = System.IO.Path.Combine(Server.MapPath("~/Upload"), saveFolder);//自定义


            UploadResult uploadReslut = null;

            if (uploadNode.fileExt == null && uploadNode.chunks == 0)
            {//上传
                uploadReslut = FileUpload.Process(uploadNode);
            }
            else if (uploadNode.fileExt == null && uploadNode.chunks > 0)
            {//分片上传
                uploadReslut = FileUpload.ProcessSplit(uploadNode);
            }
            else if (uploadNode.fileExt != null && uploadNode.chunks == 0)
            {//合并分片
                uploadReslut = FileUpload.MergeSplitFiles(uploadNode);
            }

            //string json = JsonConvert.SerializeObject(uploadReslut);
            //HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(json, Encoding.GetEncoding("UTF-8"), "application/json") };
            //return result;
            Response.Write(JsonConvert.SerializeObject(uploadReslut));
        }
コード例 #4
0
        /// <summary>
        /// 分片上传,分片文件上传1
        /// </summary>
        /// <param name="uploadNode"></param>
        /// <returns></returns>
        public static UploadResult ProcessSplit(UploadNode uploadNode)
        {//uploadNode.guid + uploadNode.id =唯一
            try
            {
                string guidFolder = Path.Combine(uploadNode.localPath, uploadNode.guid + uploadNode.id) + "/";
                string localName  = Path.Combine(guidFolder, uploadNode.chunk.ToString());
                //建立临时传输文件夹
                if (!Directory.Exists(Path.GetDirectoryName(guidFolder)))
                {
                    Directory.CreateDirectory(guidFolder);
                }

                FileStream   addFile   = new FileStream(localName, FileMode.Append, FileAccess.Write);
                BinaryWriter AddWriter = new BinaryWriter(addFile);
                //获得上传的分片数据流
                HttpPostedFileBase splitFile = uploadNode.file;
                Stream             stream    = splitFile.InputStream;

                BinaryReader TempReader = new BinaryReader(stream);
                //将上传的分片追加到临时文件末尾
                AddWriter.Write(TempReader.ReadBytes((int)stream.Length));
                //关闭BinaryReader文件阅读器
                TempReader.Close();
                stream.Close();
                AddWriter.Close();
                addFile.Close();

                return(new UploadResult()
                {
                    Status = true, LocalName = localName, chunked = uploadNode.chunked, guid = uploadNode.guid, fileExt = Path.GetExtension(uploadNode.file.FileName)
                });
            }
            catch (Exception ex)
            {
                return(new UploadResult()
                {
                    Status = false, Error = ex.ToString()
                });
            }
        }