示例#1
0
        public ActionResult OnPostSendPic()
        {
            if ((this.Request != null) && (this.Request.Form != null))
            {
                var    files    = this.Request.Form.Files;
                string PicTitle = CCommon.UnencodeQuotes(this.Request.Form.FirstOrDefault(kv => kv.Key == "picTitle").Value);
                int    imgTID   = Int32.Parse(this.Request.Form.FirstOrDefault(kv => kv.Key == "taskId").Value);
                int    imgPID   = Int32.Parse(this.Request.Form.FirstOrDefault(kv => kv.Key == "picId").Value);

                if (files != null && (files.Count == 1) && (imgTID >= 0))
                {
                    string folderName  = @"Tasks\Images";
                    string webRootPath = hostEnv.WebRootPath;
                    Pic    pic         = new Pic(imgTID, imgPID, PicTitle);
                    string newPath     = Path.Combine(webRootPath, folderName);
                    if (!Directory.Exists(newPath))
                    {
                        Directory.CreateDirectory(newPath);
                    }

                    string picFilename = "";
                    foreach (IFormFile item in files)
                    {
                        if (item.Length > 0)
                        {
                            string fileName = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName.Trim('"');
                            picFilename = pic.GetFileName(fileName);
                            string fullPath = Path.Combine(newPath, picFilename);
                            if (System.IO.File.Exists(fullPath))
                            {
                                System.IO.File.Delete(fullPath);
                            }
                            using (var stream = new FileStream(fullPath, FileMode.Create))
                            {
                                item.CopyTo(stream);
                            }
                        }
                    }
                    return(this.Content("Success:" + "Images" + "/" + picFilename));  // Send back URL
                }
            }
            return(this.Content("Fail"));
        }