示例#1
0
        public AttachmentResult Attachment(AttachmentInputModel model)
        {
            SetJobIdCookie(job.Guid);

            // Move to service/manager
            var dir = Server.MapPath(string.Format(PathToUploadsFormat, job.Guid, model.QQUUID));
            var filePath = Path.Combine(dir, model.Filename);

            try
            {
                model.SaveAs(filePath);

                job.Attachments.Add(new Attachment()
                {
                    Name = model.Filename,
                    PathToFile = string.Format(RelativePathToUploadFormat, job.Guid, model.QQUUID, model.Filename),
                    Type = new FileInfo(filePath).Extension
                });

                jobService.SaveJob(job);
            }
            catch (Exception ex)
            {
                return new AttachmentResult(false, error: ex.Message);
            }

            // the anonymous object in the result below will be convert to json and set back to the browser
            return new AttachmentResult(true, new { extraInformation = job.Guid });
        }
            public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
            {
                var request = controllerContext.RequestContext.HttpContext.Request;
                var formUpload = request.Files.Count > 0;

                // find filename
                var xFileName = request.Headers["X-File-Name"];
                var qqFile = request["qqfile"];
                var qqUuid = request["qquuid"];
                var formFilename = formUpload ? request.Files[0].FileName : null;

                var upload = new AttachmentInputModel
                {
                    Filename = xFileName ?? qqFile ?? formFilename,
                    InputStream = formUpload ? request.Files[0].InputStream : request.InputStream,
                    QQUUID = Guid.Parse(qqUuid)
                };

                return upload;
            }