private void DeliverFile(HttpContext context) { int Id = Convert.ToInt32(context.Request["Id"]); Document d = new Document(Id); string FilePath = Path.Combine(d.FilePath, d.FileName); if (File.Exists(FilePath)) { context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + d.FileName + "\""); context.Response.ContentType = "application/octet-stream"; context.Response.ClearContent(); context.Response.WriteFile(FilePath); } else context.Response.StatusCode = 404; }
// Upload entire file private void UploadWholeFile(HttpContext context, List<FilesStatus> statuses) { if (!Directory.Exists(StorageRoot)) Directory.CreateDirectory(StorageRoot); for (int i = 0; i < context.Request.Files.Count; i++) { var file = context.Request.Files[i]; var fullPath = StorageRoot + Path.GetFileName(file.FileName); if (file.FileName != null && file.FileName != "") { bool Exists = File.Exists(fullPath); fullPath = (Exists) ? fullPath.Replace(file.FileName, AppendVersion(StorageRoot, file.FileName)) : fullPath; Document = new Document() { CandidateId = (Type == QueryType.Candidate) ? this.Id : 0, ClientId = (Type == QueryType.Client) ? this.Id : 0, FileName = (Exists) ? AppendVersion(StorageRoot, file.FileName) : file.FileName, FileSize = file.ContentLength, FilePath = this.StorageRoot, UserId = this.UserId }; file.SaveAs(fullPath); Document.Save(); string fullName = Path.GetFileName(file.FileName); statuses.Add(new FilesStatus(fullName, file.ContentLength, fullPath)); } } }
// Delete file from the server private void DeleteFile(HttpContext context) { Document Document = new Document(Convert.ToInt32(context.Request["Id"])); string filePath = Document.FilePath + Document.FileName; if (File.Exists(filePath)) File.Delete(filePath); Document.Delete(); // Delete our document }