public JsonResult upload()
        {
            if (this.Request.Files.Count == 0)
            {
                return(Json(new { code = 1, msg = "no files" }));
            }

            string name     = this.Request.Form[0];
            int    block    = 0;
            int    blockNum = 0;

            int.TryParse(this.Request.Form[1], out block);
            int.TryParse(this.Request.Form[2], out blockNum);

            for (int i = 0; i < this.Request.Files.Count; i++)
            {
                HttpPostedFileBase file = this.Request.Files[i];
                var localPath           = Server.MapPath("/TempFile/" + this.Request.Form[0]);

                var fs = System.IO.File.Open(localPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
                fs.Position = fs.Length;
                Int64  size   = fs.Length;
                byte[] buffer = new byte[1024 * 1024];

                while (true)
                {
                    var reads = file.InputStream.Read(buffer, 0, buffer.Length);
                    size += reads;

                    if (reads == 0)
                    {
                        break;
                    }

                    fs.Write(buffer, 0, reads);
                }

                fs.Close();

                if (block == (blockNum - 1))
                {
                    using (MDB mdb = new MDB())
                    {
                        DbAttachment dbAttr = new DbAttachment();
                        dbAttr.Name       = name;
                        dbAttr.Size       = size;
                        dbAttr.Path       = localPath;
                        dbAttr.RemotePath = "/website/" + DateTime.Now.ToString("yyyy_MM_dd") + "/" + DateTime.Now.ToString("HH_mm_ss") + "_" + dbAttr.Name;
                        dbAttr.CreateTime = DateTime.Now;
                        dbAttr.State      = 0;
                        mdb.Insert <DbAttachment>(dbAttr);
                    }
                }
            }

            YHFSUploader.Upload();

            return(Json(new { code = 0, msg = "" }));
        }
        public static void Upload()
        {
            if (thrProcess != null)
            {
                return;
            }

            thrProcess = new Thread(() =>
            {
                try
                {
                    while (true)
                    {
                        DbAttachment f = new DbAttachment();
                        using (MDB mdb = new MDB())
                        {
                            f = mdb.Find <DbAttachment>(x => x.State == 0).FirstOrDefault();
                        }

                        if (f == null)
                        {
                            thrProcess = null;
                            return;
                        }

                        using (MDB mdb = new MDB())
                        {
                            f.State = 1;
                            mdb.UpdateOne <DbAttachment>(x => x.Id == f.Id, f);
                        }

                        YHFSClient client = new YHFSClient();
                        string log        = client.Upload(f.RemotePath, f.Path);

                        System.IO.File.AppendAllText("/wwwroot/maratonbus/log.log", log);

                        using (MDB mdb = new MDB())
                        {
                            f.State = 2;
                            mdb.UpdateOne <DbAttachment>(x => x.Id == f.Id, f);
                        }

                        System.IO.File.Delete(f.Path);
                    }
                }
                catch
                {
                    thrProcess = null;
                }
            });
            thrProcess.Start();
        }
Beispiel #3
0
        public async Task <AttachmentLink> CreateAttachment(IFormFile file)
        {
            var attachment = new DbAttachment();

            attachment.PopulateFrom(file);
            using (var stream = file.OpenReadStream()) {
                await FileService.SaveAsync($"campaigns/{attachment.Uri}", stream);
            }
            DbContext.Attachments.Add(attachment);
            await DbContext.SaveChangesAsync();

            return(new AttachmentLink {
                ContentType = file.ContentType,
                FileGuid = attachment.Guid,
                Id = attachment.Id,
                Label = file.FileName,
                PermaLink = LinkGenerator.GetUriByAction(HttpContextAccessor.HttpContext, nameof(CampaignsController.GetCampaignAttachment), CampaignsController.Name, new {
                    fileGuid = (Base64Id)attachment.Guid,
                    format = Path.GetExtension(attachment.Name).TrimStart('.')
                }),
                Size = file.Length
            });
        }