public void StartUpload() { try { //string s; foreach (string s in Request.Files) { //HttpPostedFile file = FileUpload1.PostedFile; HttpPostedFile file = Request.Files[s]; string UserID = txtusrid.Text; string imagename = file.FileName; string NamewithoutExt = Path.GetFileNameWithoutExtension(imagename); int fileSizeInBytes = file.ContentLength; Bitmap bmp = new Bitmap(file.InputStream); string imgpath = ""; using (MemoryStream m = new MemoryStream()) { bmp.Save(m, bmp.RawFormat); byte[] imageBytes = m.ToArray(); string base64String = Convert.ToBase64String(imageBytes); //return base64String; encodImg objencimg = new encodImg(); objencimg.image = base64String; objencimg.name = UserID + NamewithoutExt; //userimage.Attributes["src"] = imgpath + NamewithoutExt+".JPG"; // objencimg.UserID = HttpContext.Current.Session["UserID"].ToString(); Uri urlTemplate = new Uri(ConfigurationManager.AppSettings["ServerAddress"].ToString() + "/ImageUpload"); //Uri urlTemplate = new Uri("http://192.168.1.203:3000/ImageUpload"); //ImageUpload var javaScriptSerializer = new JavaScriptSerializer(); string jsonString = javaScriptSerializer.Serialize(objencimg); var client = new HttpClient(); client.BaseAddress = urlTemplate; client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); client.Timeout = TimeSpan.FromMilliseconds(600000); StringContent htpcontent = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = client.PostAsync("", htpcontent).Result; string resultContent = response.Content.ReadAsStringAsync().Result; if (resultContent == "0") { Console.Write("Failed"); } else { Console.Write("Uploaded successfully"); } } } } catch (Exception Ex) { throw Ex; } }
protected string UploadFile() { string strFile, strFolder, strErrmsg = "", message = ""; try { string valID = ""; foreach (string s in Request.Files) { HttpPostedFile file = Request.Files[s]; int fileSizeInBytes = file.ContentLength; string name = file.FileName; string fileName = Path.GetFileName(name); string NamewithoutExt = Path.GetFileNameWithoutExtension(name); string fileExtension = ""; if (!string.IsNullOrEmpty(fileName)) { fileExtension = Path.GetExtension(fileName); } //string newfilename = ValuationID + NamewithoutExt + fileExtension; string newfilename = valID + NamewithoutExt; filenamestring = newfilename; string strFileExt = fileExtension; int intFileSize = fileSizeInBytes; //if (strFileExt != ".jpg" && strFileExt != ".JPG" && strFileExt != ".JPG" && strFileExt != ".png" && strFileExt != ".PNG") //{ // return ""; //} //else //{ Bitmap bmp = new Bitmap(file.InputStream); using (MemoryStream m = new MemoryStream()) { bmp.Save(m, bmp.RawFormat); byte[] imageBytes = m.ToArray(); string base64String = Convert.ToBase64String(imageBytes); //return base64String; encodImg objencimg = new encodImg(); objencimg.image = base64String; objencimg.name = newfilename; Uri urlTemplate = new Uri(ConfigurationManager.AppSettings["ServerAddress"].ToString() + ":5015"); var javaScriptSerializer = new JavaScriptSerializer(); string jsonString = javaScriptSerializer.Serialize(objencimg); var client = new HttpClient(); client.BaseAddress = urlTemplate; client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); client.Timeout = TimeSpan.FromMilliseconds(600000); StringContent htpcontent = new StringContent(jsonString); var response = client.PostAsync("", htpcontent).Result; string resultContent = response.Content.ReadAsStringAsync().Result; if (resultContent == "0") { Console.Write("Failed"); } else { Console.Write("Uploaded successfully"); } } //} } return(filenamestring); } catch (Exception Ex) { throw Ex; } }
protected string UploadFile() { string strFile, strFolder, strErrmsg = "", message = ""; try { string valID = ValuationID; Auditlog("Upload function call:" + valID); foreach (string s in Request.Files) { HttpPostedFile file = Request.Files[s]; int fileSizeInBytes = file.ContentLength; string name = file.FileName; string fileName = Path.GetFileName(name); string NamewithoutExt = Path.GetFileNameWithoutExtension(name); string fileExtension = ""; if (!string.IsNullOrEmpty(fileName)) { } fileExtension = Path.GetExtension(fileName); //string newfilename = ValuationID + NamewithoutExt + fileExtension; //newfilename = ValuationID + NamewithoutExt; string newfilename = HttpContext.Current.Request.Form["fileName"]; newfilename = ValuationID + newfilename; string strFileExt = fileExtension; int intFileSize = fileSizeInBytes; System.IO.Stream fs = file.InputStream; System.IO.BinaryReader br = new System.IO.BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); encodImg objencimg = new encodImg(); objencimg.image = base64String; objencimg.name = newfilename; objencimg.extension = fileExtension; Uri urlTemplate = new Uri(ConfigurationManager.AppSettings["ServerAddress"].ToString() + "/ImageUpload"); //Uri urlTemplate = new Uri("http://localhost" + ":3000/ImageUpload"); //ImageUpload Auditlog("Upload function newfilename:" + newfilename); var javaScriptSerializer = new JavaScriptSerializer(); string jsonString = javaScriptSerializer.Serialize(objencimg); var client = new HttpClient(); client.BaseAddress = urlTemplate; client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); client.Timeout = TimeSpan.FromMilliseconds(600000); StringContent htpcontent = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = client.PostAsync("", htpcontent).Result; string resultContent = response.Content.ReadAsStringAsync().Result; if (resultContent == "0") { Auditlog("Upload function Failed"); Console.Write("Failed"); } else { Auditlog("Upload function successfully"); Console.Write("Uploaded successfully"); } // Byte[] bytes = File.ReadAllBytes("path"); //String file = Convert.ToBase64String(bytes); //Bitmap bmp = new Bitmap(file.InputStream); //using (MemoryStream m = new MemoryStream()) ////using (FileStream file = new FileStream("file.bin", FileMode.Create, System.IO.FileAccess.Write)) //{ // bmp.Save(m, bmp.RawFormat); // byte[] imageBytes = m.ToArray(); // string base64String = Convert.ToBase64String(imageBytes); // //return base64String; // encodImg objencimg = new encodImg(); // objencimg.image = base64String; // objencimg.name = newfilename; // Uri urlTemplate = new Uri(ConfigurationManager.AppSettings["ServerAddress"].ToString() + "/ImageUpload"); // //Uri urlTemplate = new Uri("http://localhost" + ":3000/ImageUpload"); // //ImageUpload // Auditlog("Upload function newfilename:" + newfilename); // var javaScriptSerializer = new JavaScriptSerializer(); // string jsonString = javaScriptSerializer.Serialize(objencimg); // var client = new HttpClient(); // client.BaseAddress = urlTemplate; // client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); // client.Timeout = TimeSpan.FromMilliseconds(600000); // StringContent htpcontent = new StringContent(jsonString, Encoding.UTF8, "application/json"); // var response = client.PostAsync("", htpcontent).Result; // string resultContent = response.Content.ReadAsStringAsync().Result; // if (resultContent == "0") // { // Auditlog("Upload function Failed"); // Console.Write("Failed"); // } // else // { // Auditlog("Upload function successfully"); // Console.Write("Uploaded successfully"); // } //} } return(filenamestring); } catch (Exception Ex) { throw Ex; } }