Exemple #1
0
        // 文件上传完毕时操作
        void fileUpload_FileUploadCompleted(object sender, FileUploadCompletedEventArgs args)
        {
            string fileName = "";

            OuterServiceFO fsFO = new OuterServiceFO();

            try
            {
                FileInfo fi = new FileInfo(args.FilePath);
                string   completedFileName = UploadFileInfo.GetAppCompletedFileName(fileInfo.FileName, DateTime.Now);
                string   targetFile        = Path.Combine(fi.Directory.FullName, completedFileName);


                string relateId = HttpContext.Current.Request.Form["RelateId"];
                string src      = HttpContext.Current.Request.Form["Src"];

                long fileSize = fi.Length;

                fileName = fsFO.AddLocalFile(fi.FullName, completedFileName, relateId, "", "", src, true);

                ctx.Response.Write(fileName + "_" + fileSize.ToString());
            }
            catch (Exception exp)
            {
                if (fileName != "")
                {
                    fsFO.DelFiles(fileName, string.Format("Upload页面错误,{0}", exp.Message).Substring(0, 200));
                    FileInfo file = new FileInfo(args.FilePath);
                    file.Delete();
                }

                ctx.Response.StatusCode = 500;
                ctx.Response.Write("error:" + exp.Message);
            }


            this.WriteLog("Upload End");
        }
Exemple #2
0
        public void ProcessRequest(HttpContext context, string uploadPath, UploadFileInfo fileInfo, UploadConfig uploadConfig)
        {
            // 是否已上传完毕
            bool complete = string.IsNullOrEmpty(context.Request.Form["Complete"]) ? true : bool.Parse(context.Request.Form["Complete"]);
            // 获取上传进度操作标识
            bool getBytes = string.IsNullOrEmpty(context.Request.Form["GetBytes"]) ? false : bool.Parse(context.Request.Form["GetBytes"]);
            // 上传开始位置,用于断点续传
            long startByte = string.IsNullOrEmpty(context.Request.Form["StartByte"]) ? 0 : long.Parse(context.Request.Form["StartByte"]);

            // 服务器端文件路径
            string filePath = Path.Combine(uploadPath, fileInfo.FileString);

            FileHelper.CheckFileFullPath(filePath);

            if (getBytes)
            {
                FileInfo fi = new FileInfo(HttpContext.Current.Server.UrlDecode(filePath));

                if (!fi.Exists)
                {
                    context.Response.Write("0");
                }
                else
                {
                    context.Response.Write(fi.Length.ToString());
                }

                context.Response.Flush();
                return;
            }
            else
            {
                if (startByte > 0 && File.Exists(filePath))
                {
                    using (FileStream fs = File.Open(filePath, FileMode.Append))
                    {
                        SaveFile(context.Request.Files["Filedata"].InputStream, fs);
                        fs.Close();
                    }
                }
                else
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                    using (FileStream fs = File.Create(filePath))
                    {
                        SaveFile(context.Request.Files["Filedata"].InputStream, fs);
                        fs.Close();
                    }
                }
                if (complete)
                {
                    if (FileUploadCompleted != null)
                    {
                        FileUploadCompletedEventArgs args = new FileUploadCompletedEventArgs(fileInfo.FileName, filePath);
                        FileUploadCompleted(this, args);
                    }
                }
            }
        }