예제 #1
0
        [HttpPost] // Fetches the userfile from the database that the user uloaded and passes it through the controller
        public JsonResult UserFileUpload(int _FileShareStatusID)
        {
            try
            {
                foreach (string file in Request.Files)
                {
                    var fileContent = Request.Files[file];
                    if (fileContent != null && fileContent.ContentLength > 0)
                    {
                        try
                        {
                            byte[] uploadedFile = new byte[fileContent.InputStream.Length];
                            fileContent.InputStream.Read(uploadedFile, 0, uploadedFile.Length);
                            string   xddd   = Path.GetFileName(fileContent.FileName);
                            int      length = fileContent.FileName.Length;
                            string[] Name   = fileContent.FileName.Split('.');

                            Models.File newfile = new Models.File();

                            Models.File std = new Models.File()
                            {
                                ContentType          = fileContent.ContentType,
                                CurrentVersionNumber = 1,
                                DateCreated          = DateTime.Now,
                                FileImage            = uploadedFile,
                                FileName             = Path.GetFileNameWithoutExtension(fileContent.FileName),
                                FileSize             = fileContent.ContentLength,
                                ParentFileID         = 0, //default value
                                UserIDOfFileOwner    = User.Identity.GetUserId(),
                                UserIDOfLastUploaded = User.Identity.GetUserId(),
                                FileLookupStatusID   = (int)FileViewStatus.FileIsAvailable,
                                FileShareStatusID    = _FileShareStatusID,
                                FileExtension        = Path.GetExtension(fileContent.FileName).Replace(".", "")
                            };
                            db.Files.Add(std);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException dbEx)
                            {
                                foreach (DbEntityValidationResult entityErr in dbEx.EntityValidationErrors)
                                {
                                    foreach (DbValidationError error in entityErr.ValidationErrors)
                                    {
                                        Console.WriteLine("Error Property Name {0} : Error Message: {1}",
                                                          error.PropertyName, error.ErrorMessage);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            ViewBag.Message = "ERROR:" + ex.Message.ToString();
                        }
                    }
                }
            }
            catch (Exception)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json("Upload failed"));
            }

            return(Json("File uploaded successfully"));
        }
예제 #2
0
        public ActionResult UploadUserFile(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    byte[] uploadedFile = new byte[file.InputStream.Length];
                    file.InputStream.Read(uploadedFile, 0, uploadedFile.Length);

                    int      length = file.FileName.Length;
                    string[] Name   = file.FileName.Split('.');

                    File newfile = new File();

                    File std = new File()
                    {
                        ContentType          = file.ContentType,
                        CurrentVersionNumber = 1,
                        DateCreated          = DateTime.Now,
                        FileImage            = uploadedFile,
                        FileName             = Name[0],
                        FileSize             = file.ContentLength,
                        ParentFileID         = 0,
                        UserIDOfFileOwner    = User.Identity.GetUserId(),
                        UserIDOfLastUploaded = User.Identity.GetUserId(),
                        FileLookupStatusID   = (int)FileViewStatus.FileIsAvailable,
                        FileShareStatusID    = (int)FileSharedStatus.Private,
                        FileExtension        = Name[1]
                    };
                    db.Files.Add(std);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (DbEntityValidationResult entityErr in dbEx.EntityValidationErrors)
                        {
                            foreach (DbValidationError error in entityErr.ValidationErrors)
                            {
                                Console.WriteLine("Error Property Name {0} : Error Message: {1}",
                                                  error.PropertyName, error.ErrorMessage);
                            }
                        }
                    }



                    ViewBag.Message = "File uploaded successfully";
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }
            else
            {
                ViewBag.Message = "You have not specified a file.";
            }

            return(RedirectToAction("ShowUserDocs"));
        }