如何将base64字符串转换为图像二进制文件并保存到服务器

作为一个例子,我已经转换了一个带有重新resize的图像的canvas元素,并将其发布到一个现在编码为的隐藏输入字段中

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD... 

然后将此值发布到我需要将此字符串转换为图像并保存到服务器上的同一页面。

代码背后的文件(upload.aspx)

 protected void btnUpload_Click(object sender, EventArgs e) { HttpPostedFile filePosted = Request.Files["newinput"]; string base64String = filePosted.ToString(); // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); //I DONT KNOW HOW TO WRITE ABOVE INTO THE SaveAs CONDITION BELOW if (filePosted != null && filePosted.ContentLength > 0) { string fileNameApplication = Path.GetFileName(filePosted.FileName); string fileExtensionApplication = Path.GetExtension(fileNameApplication); // generating a random guid for a new file at server for the uploaded file string newFile = Guid.NewGuid().ToString() + fileExtensionApplication; // getting a valid server path to save string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile); if (fileNameApplication != String.Empty) { filePosted.SaveAs(filePath); } } 

我很确定在保存到服务器之前我需要将imagedata转换为二进制文件,但我不能完全理解我需要修改上面的代码。 有任何想法吗? 保存到服务器的代码不起作用。

一旦我将其转换为图像并更改了上面的名称 – 我将其通过LINQ存储回数据库 – 并附加一个URL。

任何帮助将不胜感激。

希望以下function有所帮助

 public string ImageToBase64(Image image, System.Drawing.Imaging.ImageFormat format) { using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[] image.Save(ms, format); byte[] imageBytes = ms.ToArray(); // Convert byte[] to Base64 String string base64String = Convert.ToBase64String(imageBytes); return base64String; } } public Image Base64ToImage(string base64String) { // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); Image image = Image.FromStream(ms, true); return image; } 

编辑1 –

从评论看来,你似乎得到了base64字符串,你需要将它保存为服务器上的图像,然后在需要时你需要使用物理服务器路径显示该图像。

好。 Base64ToImage将为您提供base64字符串的图像。 您可以使用保存在服务器上

 image.Save("PATH", System.Drawing.Imaging.ImageFormat.Jpeg); 

您提供或创建的“PATH”可以作为URL存储在DB中,您可以在显示时使用。

注意:确保您对要保存图像的文件夹具有写入权限。

编辑-2您的function应如下所示。 请根据需要输入validation码,error handling。

 protected void btnUpload_Click(object sender, EventArgs e) { HttpPostedFile filePosted = Request.Files["newinput"]; string base64String = filePosted.ToString(); // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String); MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length); // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true); string newFile = Guid.NewGuid().ToString() + fileExtensionApplication; string filePath = Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile); image.Save(filepath,ImageFormat.Jpeg); }