Base64 Encode and Decode any file using Postman and web API
namespace
===============================
using System.Net.Http;
using System.Web;
using System.Web.Http;
Action
===============================
[HttpPost]
public HttpResponseMessage updateprofileImage()
{
HttpResponseMessage response = new HttpResponseMessage();
var httpRequest = HttpContext.Current.Request;
string FileName1 = string.Empty;
string base64 = string.Empty;
String file64 = string.Empty;
if (httpRequest.Files.Count > 0)
{
HttpPostedFile file = httpRequest.Files[0];
string fileOriginalPath = string.Empty;
fileOriginalPath = "~/FileUploaded";
string strTutorImg = string.Empty;
if (file != null)
{
string Customerfile;
Customerfile = DateTime.UtcNow.Ticks + "-" + 'C' + "-" + file.FileName;
var filepath = httpRequest.MapPath(fileOriginalPath);
var base63 = httpRequest.MapPath(base64);
if (!Directory.Exists(filepath))
Directory.CreateDirectory(filepath);
filepath = Path.Combine(filepath, System.IO.Path.GetFileName(Customerfile));
file.SaveAs(filepath);
/*Convert Pdf into base64 formate start here(Encode)*/
Byte[] bytes = File.ReadAllBytes(filepath);
file64 = Convert.ToBase64String(bytes);
/*end here*/
/*where you need to save 64 file*/
string Base64file;
Base64file = DateTime.UtcNow.Ticks + "-" + "base64-" + "decod.pdf";
var filepath1 = httpRequest.MapPath(fileOriginalPath);
filepath1 = Path.Combine(filepath1, System.IO.Path.GetFileName(Base64file));
/*end here */
/*Convert base64 file into pdf file start here(Decode)*/
Byte[] bytes2 = Convert.FromBase64String(file64);
File.WriteAllBytes(filepath1, bytes2);
/*End here*/
FileName1 = file.FileName;
var CustomerImage1 = Customerfile;
}
}
return Request.CreateResponse(HttpStatusCode.OK, file64); ;
}
No comments:
Post a Comment