Пример #1
0
        async static Task <PFile> DeserializeAndOpenAsync(string path, FileMode mode, FileAccess access)
        {
            using (Stream source = await OpenFileAsync(path, mode, access))
            {
                byte [] lengthArray = new byte[sizeof(int)];
                await source.ReadAsync(lengthArray, 0, lengthArray.Length);

                int jsonLength = BitConverter.ToInt32(lengthArray, 0);

                byte[] jsonArray = new byte[jsonLength];
                await source.ReadAsync(jsonArray, 0, jsonArray.Length);

                DataContractJsonSerializer jserializer = new DataContractJsonSerializer(typeof(PFile));
                Stream jsonStream = new MemoryStream();
                await jsonStream.WriteAsync(jsonArray, 0, jsonArray.Length);

                jsonStream.Seek(0, SeekOrigin.Begin);
                PFile result = (PFile)jserializer.ReadObject(jsonStream);

                byte[] imageBuffer = new byte[source.Length - source.Position];
                await source.ReadAsync(imageBuffer, 0, imageBuffer.Length);

                result.Data = new MemoryStream();
                await result.Data.WriteAsync(imageBuffer, 0, imageBuffer.Length);

                result.Data.Seek(0, SeekOrigin.Begin);
                return(result);// (T)(jserializer.ReadObject(source));
            }
        }
Пример #2
0
        public async Task <IActionResult> DownloadReview(string pFileId)
        {
            AppUser user = await userManager.GetUserAsync(HttpContext.User);

            if (pFileId == null)
            {
                return(Content("filename not present"));
            }
            downloadFile = pFileRepo.FindByID(pFileId);
            if (user != downloadFile.AppUser)
            {
                return(Content("file not available to you"));
            }
            string pathToFile = "Data/" + downloadFile.ID + "." + downloadFile.Ext;
            Stream memory     = new MemoryStream();

            using (var stream = new FileStream(pathToFile, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;

            string downFileName = downloadFile.Name + "." + downloadFile.Ext;

            return(File(memory, GetContentType(), downFileName));
        }
Пример #3
0
 static Task <bool> CreatePhotoAlbumAsync(string albumName, Stream thumbPicture)
 {
     return(Task.Run(async() =>
     {
         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
         {
             if (file.DirectoryExists(Path.Combine(Directories.AlbumRoot, albumName)))
             {
                 thumbPicture.Dispose();
                 return false;
             }
             file.CreateDirectory(Path.Combine(Directories.AlbumRoot, albumName));
             PFile album = new PFile();
             album.Name = albumName;
             album.Path = Path.Combine(Directories.Thumbnails, albumName);
             using (thumbPicture)
             {
                 byte [] buffer = new byte[thumbPicture.Length];
                 thumbPicture.Seek(0, SeekOrigin.Begin);
                 await thumbPicture.ReadAsync(buffer, 0, buffer.Length);
                 album.Data = new MemoryStream();
                 await album.Data.WriteAsync(buffer, 0, buffer.Length);
             }
             await SerializeAndSaveFile <PFile>(album, album.Path);
             return true;
         }
     }));
 }
Пример #4
0
 /// <summary>
 /// Saves the file header to disk.
 /// </summary>
 internal override void SaveHeader(CodeWriter writer, PFile file)
 {
     writer.Write(file.Name);
     writer.Write(new FileInfo(file.FilePath).Length);
     writer.Write(file.LastModified.Ticks);
     writer.Write((int) file.Attributes);
 }
Пример #5
0
        public async Task <IActionResult> Upload(List <IFormFile> files)
        {
            PFile  newPFile;
            Stream stream;
            Guid   guidFileId;
            long   size = files.Sum(f => f.Length);

            foreach (var formFile in files)
            {
                guidFileId = Guid.NewGuid();
                string  ext  = formFile.FileName.Split(".")[1];
                string  name = formFile.FileName.Split(".")[0];
                AppUser user = await userManager.GetUserAsync(HttpContext.User);

                string destinationFolder = "Data/" + guidFileId + "." + ext;


                if (formFile.Length > 0)
                {
                    using (stream = new FileStream(destinationFolder, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }
                }
                newPFile = new PFile(guidFileId.ToString(), name, ext, user);
                pFileRepo.Add(newPFile);
                pFiles = pFileRepo.GetAll();
            }

            // process uploaded files
            // Don't rely on or trust the FileName property without validation.

            return(Ok(new { count = files.Count, size, })); //filePath
        }
        public async Task <JsonResult> UploadStudentAssignment(List <IFormFile> files, int courseAssignmentId)
        {
            SetRoles();
            PFile             newPFile;
            CourseAssignment  courseAssignment;
            StudentAssignment studentAssignment;
            JsonResponse <StudentAssignment> response = new JsonResponse <StudentAssignment>();
            Stream stream;
            Guid   guidFileId;
            long   size = files.Sum(f => f.Length);

            foreach (var formFile in files)
            {
                guidFileId = Guid.NewGuid();
                string  ext  = formFile.FileName.Split(".")[1];
                string  name = formFile.FileName.Split(".")[0];
                AppUser user = await userManager.GetUserAsync(HttpContext.User);

                courseAssignment = courseAssignmentRepo.FindByID(courseAssignmentId);

                string destinationFolder = "Data/" + guidFileId + "." + ext;


                if (formFile.Length > 0)
                {
                    using (stream = new FileStream(destinationFolder, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }

                    //Create the PFile and add it to the student assignment
                    newPFile = new PFile(guidFileId.ToString(), name, ext, user);
                    pFileRepo.Add(newPFile);
                    studentAssignment = new StudentAssignment()
                    {
                        CourseAssignment = courseAssignment, AppUser = user, FK_PFile = newPFile, TimestampCreated = System.DateTime.Now
                    };

                    //Create the assignment and add it to the response
                    string jsonStudentAssignment = JsonConvert.SerializeObject(await CreateStudentAssignment(studentAssignment));
                    studentAssignment = JsonConvert.DeserializeObject <StudentAssignment>(jsonStudentAssignment);
                    response.Data.Add(studentAssignment);
                }
                else
                {
                    response.Error.Add(new Error("No File", "No file to upload"));
                    return(Json(response));
                }
            }
            return(Json(response));
        }
Пример #7
0
        public async Task <JsonResult> UploadReview(List <IFormFile> files, int studentAssignmentId)
        {
            PFile             newPFile;
            StudentAssignment studentAssignment;
            Review            newReview;
            Stream            stream;
            Guid guidFileId;
            long size = files.Sum(f => f.Length);

            foreach (var formFile in files)
            {
                guidFileId = Guid.NewGuid();
                string  ext  = formFile.FileName.Split(".")[1];
                string  name = formFile.FileName.Split(".")[0];
                AppUser user = await userManager.GetUserAsync(HttpContext.User);

                studentAssignment = studentAssignmentRepo.FindByID(studentAssignmentId);

                string destinationFolder = "Data/" + guidFileId + "." + ext;


                if (formFile.Length > 0)
                {
                    using (stream = new FileStream(destinationFolder, FileMode.Create))
                    {
                        await formFile.CopyToAsync(stream);
                    }

                    newPFile = new PFile(guidFileId.ToString(), name, ext, user);
                    pFileRepo.Add(newPFile);
                    newReview = new Review()
                    {
                        FK_STUDENT_ASSIGNMENT = studentAssignment, FK_APP_USER = user, FK_PFile = newPFile, TimestampCreated = System.DateTime.Now
                    };

                    newReview = await CreateReview(newReview);

                    response.Data.Add(newReview);
                }
                else
                {
                    response.Error.Add(new Error("No File", "No file to upload"));
                    return(Json(response));
                }
            }
            return(Json(response));
        }
Пример #8
0
        public JsonResult DeleteReview(string id)
        {
            JsonResponse <string> jsonResponse = new JsonResponse <string>();

            downloadFile = pFileRepo.FindByID(id);
            System.IO.File.Delete("Data/" + downloadFile.ID + "." + downloadFile.Ext);

            if (pFileRepo.Delete(downloadFile) == true)
            {
                jsonResponse.Data.Add("File Deleted");
                return(Json(jsonResponse));
            }
            jsonResponse.Error.Add(new Error()
            {
                Name = "Not Deleted", Description = "File not deleted"
            });
            return(Json(jsonResponse));
        }
Пример #9
0
        protected void fff(object sender, EventArgs e)
        {
            HttpPostedFile postedFile = Request.Files[0];

            PFileVar ff = new PFileVar
            {
                IsSaveOld = true,
            };

            PFileVar pfv = new PFileVar();

            pfv.File_Upload  = postedFile;
            pfv.PathRelative = "/pf/";

            pfv.IsWatermark       = true;
            pfv.WatermarkPath     = Request.MapPath("/images/sy/clz.png");
            pfv.IsSaveOld         = true;
            pfv.SaveOldPathPrefix = "/pf#old/";

            pfv.IsThumbnail   = true;
            pfv.ThumbnailWith = 250;
            pfv.XLType        = PFileVar.ThumPicType.限制宽;

            pfv.IsWatermarkThum   = true;
            pfv.WatermarkPathThum = Request.MapPath("/images/sy/clz.png");



            PFile pf = new PFile(pfv);

            //开始上传
            if (pf.Upload_File())
            {
                Response.Write(pf.GetRelativeAllPath + "<br>"); //大图路径
                Response.Write(pf.GetRelativeAllPathThum);      //小图路径
            }
            else
            {
                Response.Write(pf.ReturnMessage);//上传失败的错误信息
            }
        }
Пример #10
0
 /// <summary>
 /// Adds the given file to this folder.
 /// </summary>
 public void Add(PFile file)
 {
     this._files.Add(file);
     file.ParentFolder = this;
 }
Пример #11
0
        public async Task <JsonResult> CreateAssignment(int courseID, string assignmentname, List <IFormFile> files,
                                                        string dueDate)
        {
            SetRoles();
            //   DateTime.ParseExact(dueDate,"mm/DD/YYYY");
            JsonResponse <CourseAssignment> response = new JsonResponse <CourseAssignment>();
            AppUser user = await userManager.GetUserAsync(HttpContext.User);

            Course course   = courseRepository.FindByID(courseID);
            PFile  newPFile = new PFile();

            if (course != null)
            {
                Stream stream;
                Guid   guidFileId;
                long   size = files.Sum(f => f.Length);
                foreach (var formFile in files)
                {
                    guidFileId = Guid.NewGuid();
                    string ext      = formFile.FileName.Split(".")[1];
                    string filename = formFile.FileName.Split(".")[0];
                    //AppUser user = await userManager.GetUserAsync(HttpContext.User);

                    string destinationFolder = "Data/" + guidFileId + "." + ext;


                    if (formFile.Length > 0)
                    {
                        using (stream = new FileStream(destinationFolder, FileMode.Create))
                        {
                            await formFile.CopyToAsync(stream);
                        }
                    }
                    newPFile = new PFile(guidFileId.ToString(), filename, ext, user);
                    pFileRepo.Add(newPFile);
                    pFiles = pFileRepo.GetAll();
                }
                if (this.isAdmin || this.isInstructor && course.FK_INSTRUCTOR.Id == user.Id)
                {
                    if (ModelState.IsValid)
                    {
                        CourseAssignment newAssignment = new CourseAssignment
                        {
                            Name      = assignmentname,
                            FK_COURSE = course,
                            PFile     = newPFile,
                            DueDate   = DateTime.Parse(dueDate)
                        };

                        if (courseAssignmentRepository.Add(newAssignment) != null)
                        {
                            response.Data.Add(newAssignment);
                            return(Json(response));
                        }
                        else
                        {
                            response.Error.Add(new Error("NotSuccessful", "The data was not successfully written."));
                        }
                    }
                }
                else
                {
                    response.Error.Add(new Error("Forbidden", "You are not allowed here naive."));
                }
            }
            else
            {
                response.Error.Add(new Error("NotFound", "The course was not found."));
            }
            return(Json(response));
        }
Пример #12
0
 /// <summary>
 /// 转发文件传输消息
 /// </summary>
 /// <param name="FileMsg"></param>
 /// <param name="session"></param>
 private void onPFile(PFile msg, string XMLMsg, TCPServerSession session)
 {
     SendMessageToUser(msg.to, XMLMsg);
 }
Пример #13
0
 public void Add(string file, PFile pFile)
 {
     files.Add(file, pFile);
 }
Пример #14
0
 /// <summary>
 /// 转发文件传输消息
 /// </summary>
 /// <param name="FileMsg"></param>
 /// <param name="session"></param>
 private void onPFile(PFile msg, string XMLMsg, TCPServerSession session)
 {
     SendMessageToUser(msg.to, XMLMsg);
 }