public void linkToTblattachment(int issueID, HttpPostedFileBase postedFile) { string fname; fname = postedFile.FileName; string folderName = WebConfigurationManager.AppSettings["taskItUploads"].ToString(); var path = Path.Combine(folderName, fname); postedFile.SaveAs(path); string sItemid = issueID.ToString(); string sDateTime = DateTime.Now.ToString(); string sIdDateTime = string.Concat(sItemid, sDateTime); if (ModelState.IsValid) { tblFileAttachment fileAttachment = new tblFileAttachment(); fileAttachment.originalFileName = fname; fileAttachment.itemID = issueID; fileAttachment.createdDate = DateTime.Now; fileAttachment.objectTypeID = 11; fileAttachment.savedAsFileName = string.Concat(sIdDateTime, fname); pocoDb.Insert(fileAttachment); } }
public ActionResult fileDownload(int ID) { string fileExtension = ""; FileStream fs; String strPath = WebConfigurationManager.AppSettings["taskItUploads"].ToString(); String strFileName = ""; var fileAttachments = pocoDb.Fetch <tblFileAttachment>(" where fileID = @0", ID).FirstOrDefault(); tblFileAttachment file = new tblFileAttachment(); strFileName = fileAttachments.originalFileName; fs = System.IO.File.Open(strPath + strFileName, FileMode.Open); fileExtension = strFileName.Substring(strFileName.LastIndexOf(".")).ToLower(); fileExtension = fileExtension.Replace(".", ""); fileExtension = Path.GetExtension(strFileName).ToLower(); Byte[] bytBytes = new Byte[fs.Length]; fs.Read(bytBytes, 0, (Int32)fs.Length); fs.Close(); string strHeader = ""; string strResponseContentType = ""; switch (fileExtension) { case ".jpg": strHeader = ""; strResponseContentType = "image/jpeg"; break; case ".png": strHeader = ""; strResponseContentType = "image/png"; break; case ".gif": strHeader = ""; strResponseContentType = "image/gif"; break; default: strHeader = "attachment; filename=" + strFileName; strResponseContentType = "application/octet-stream"; break; } if (strHeader != "") { Response.AddHeader("Content-disposition", strHeader); } Response.ContentType = strResponseContentType; Response.BinaryWrite(bytBytes); Response.End(); return(View()); }
public JsonResult UploadImage(int itemId, int objectTypeId, string imageData) { int userPersonID = 0; try { var fileName = String.Format("screenshot-{0:d-MM-yy}.png", DateTime.Today); string userPerson = ""; if (Session["userId"] != null) { userPerson = Session["userId"].ToString(); } userPersonID = int.Parse(userPerson); tblFileAttachment fileAttachment = new tblFileAttachment(); fileAttachment.originalFileName = fileName; fileAttachment.savedAsFileName = itemId.ToString() + " " + DateTime.Now.ToString("yyyyMMdd hhmmss ") + fileName; fileAttachment.itemID = itemId; fileAttachment.objectTypeID = objectTypeId; fileAttachment.createdDate = DateTime.Now; fileAttachment.createdByID = userPersonID; fileAttachment.Save(); string folderName = WebConfigurationManager.AppSettings["taskItUploads"].ToString(); var path = Path.Combine(folderName, fileAttachment.originalFileName); using (FileStream fs = new FileStream(path, FileMode.Create)) { using (BinaryWriter bw = new BinaryWriter(fs)) { byte[] data = Convert.FromBase64String(imageData); bw.Write(data); bw.Close(); } } } catch (UnauthorizedAccessException ex) { return(Json(ex.Message + "Permission to upload file denied")); } catch (Exception ex) { return(Json("Upload failed: " + ex.Message)); } var lastAttachment = pocoDb.Fetch <tblFileAttachment>("where itemID=@0 and objectTypeID=@1 and createdByID=@2", 0, 11, userPersonID).LastOrDefault(); var screenshotID = lastAttachment.fileID; Session["screenshotid"] = screenshotID; return(Json("File uploaded successfully")); }