/// <summary> /// Add a file to the signature request by giving its remote address. /// </summary> public void AddFile(Uri uri) { if (Files.Count > 0) { throw new NotSupportedException("Cannot add local and remote files in the same request"); } if ((uri.Scheme != "http") && (uri.Scheme != "https")) { throw new ArgumentException("Only HTTP or HTTPS URIs are allowed"); } FileUrls.Add(uri.AbsoluteUri); }
/// <summary> /// Saves file to the server. /// </summary> /// <param name="oFile">Posted FileBase</param> /// <param name="sUrl">Relative Save Path</param> /// <param name="bOverwrite">Overwrite</param> /// <returns>bool</returns> public bool SaveFile(HttpPostedFileBase oFile, string sUrl, bool bOverwrite) { try { bool bStatus = false; string sServerFileUrl = String.Empty; //Check if the posted filebase has file if (oFile != null) { if (bOverwrite) { //Gets the filename of the file. string sFileName = Path.GetFileName(oFile.FileName); //Combines the url and the new filename. string sPath = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sFileName); //Save the file with a new filename. if (File.Exists(sPath)) { File.Delete(sPath); } oFile.SaveAs(sPath); //Add a new url so the user can get the server url of the file. sServerFileUrl = sUrl + "/" + oFile.FileName; FileUrls.Add(sServerFileUrl); bStatus = true; } else { //Gets the extension of the file. string sExtension = Path.GetExtension(oFile.FileName); //Combines the url and the new filename. string sName = Guid.NewGuid().ToString("N") + sExtension; string sPath = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sName); //Save the file with a new filename. oFile.SaveAs(sPath); //Add a new url so the user can get the server url of the file. sServerFileUrl = sUrl + "/" + sName; FileUrls.Add(sServerFileUrl); bStatus = true; } } return(bStatus); } catch (Exception ex) { Errors.Add(new Error { Message = ex.Message, Exception = ex }); return(false); } }
/// <summary> /// Saves file to the server. /// </summary> /// <param name="sDataString">Posted 64 Bit data string</param> /// <param name="sFileName">File Name</param> /// <param name="sUrl">Prefix</param> /// <param name="bOverwrite">OverWrite</param> /// <param name="sPrefix">Prefix</param> /// <param name="sSuffix">Suffix</param> /// <param name="iWidth">Image Width Size</param> /// <param name="iHeight">Image Height Size</param> /// <param name="bThumb">Is Thumbnail</param> /// <returns>bool</returns> public bool SaveBase64File(string sDataString, string sFileName, string sUrl, bool bOverwrite, string sPrefix, string sSuffix, int iWidth = 0, int iHeight = 0, bool bThumb = false) { // copied from https://gist.github.com/vbfox/484643 var base64Data = Regex.Match(sDataString, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value; var binData = Convert.FromBase64String(base64Data); bool sStatus = false; string sServerFileUrl = String.Empty; using (var stream = new MemoryStream(binData)) { var vImage = Image.FromStream(stream); int iNewWidth = vImage.Width; int iNewHeight = vImage.Height; //Start creating new bitmap with the new dimension. var vImageBithmap = new Bitmap(iWidth, iHeight); //Start creating graphic effect for smoothing the file. var vImageGraph = Graphics.FromImage(vImageBithmap); vImageGraph.CompositingQuality = CompositingQuality.HighQuality; vImageGraph.SmoothingMode = SmoothingMode.HighQuality; //Start creating Rectangle to contain the image. var vImageRec = new Rectangle(0, 0, iWidth, iHeight); vImageGraph.DrawImage(vImage, vImageRec); //Save the image with the new Size. string sFileUrl = String.Empty; string sNFileName = String.Empty; if (bOverwrite) { sNFileName = sPrefix + Path.GetFileNameWithoutExtension(sFileName) + sSuffix + Path.GetExtension(sFileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sNFileName); if (File.Exists(sFileUrl)) { File.Delete(sFileUrl); } vImageBithmap.Save(sFileUrl, vImage.RawFormat); } else { sNFileName = sPrefix + Guid.NewGuid().ToString("N") + sSuffix + Path.GetExtension(sFileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sNFileName); vImageBithmap.Save(sFileUrl, vImage.RawFormat); } //Add a new url so the user can get the server url of the file. sServerFileUrl = sUrl + "/" + sNFileName; if (bThumb) { ThumbUrls.Add(sServerFileUrl); } else { FileUrls.Add(sServerFileUrl); } //Dispose all used objects. vImageGraph.Dispose(); vImageBithmap.Dispose(); vImage.Dispose(); sStatus = true; } return(sStatus); }
/// <summary> /// Saves file to the server. /// </summary> /// <param name="oFile">Posted FileBase</param> /// <param name="sUrl">Relative Save Path</param> /// <param name="iWidth">Image Width Size</param> /// <param name="iHeight">Image Height Size</param> /// <param name="bOverwrite">Overwrite</param> /// <param name="bThumb">Make it as a thumbnail</param> /// <returns>bool</returns> public bool SaveFile(HttpPostedFileBase oFile, string sUrl, int iWidth, int iHeight, bool bOverwrite, bool bThumb, string sPrefix = "", string sSuffix = "") { try { bool sStatus = false; string sServerFileUrl = String.Empty; //Check if the filebase has content. if (oFile != null) { //Create the image from filebase to Image Drawing. var vImage = Image.FromStream(oFile.InputStream); //Start creating new bitmap with the new dimension. var vImageBithmap = new Bitmap(iWidth, iHeight); //Start creating graphic effect for smoothing the file. var vImageGraph = Graphics.FromImage(vImageBithmap); vImageGraph.CompositingQuality = CompositingQuality.HighQuality; vImageGraph.SmoothingMode = SmoothingMode.HighQuality; //Start creating Rectangle to contain the image. var vImageRec = new Rectangle(0, 0, iWidth, iHeight); vImageGraph.DrawImage(vImage, vImageRec); //Save the image with the new Size. string sFileUrl = String.Empty; string sFileName = String.Empty; if (bOverwrite) { sFileName = sPrefix + Path.GetFileNameWithoutExtension(oFile.FileName) + sSuffix + Path.GetExtension(oFile.FileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sFileName); if (File.Exists(sFileUrl)) { File.Delete(sFileUrl); } vImageBithmap.Save(sFileUrl, vImage.RawFormat); } else { sFileName = sPrefix + Guid.NewGuid().ToString("N") + sSuffix + Path.GetExtension(oFile.FileName); sFileUrl = Path.Combine(HttpContext.Current.Server.MapPath(sUrl), sFileName); vImageBithmap.Save(sFileUrl, vImage.RawFormat); } //Add a new url so the user can get the server url of the file. sServerFileUrl = sUrl + "/" + sFileName; if (bThumb) { ThumbUrls.Add(sServerFileUrl); } else { FileUrls.Add(sServerFileUrl); } //Dispose all used objects. vImageGraph.Dispose(); vImageBithmap.Dispose(); vImage.Dispose(); sStatus = true; } return(sStatus); } catch (Exception ex) { Errors.Add(new Error() { Message = ex.Message, Exception = ex }); return(false); } }