【问题标题】:Saving HTML of img/image to Disk将 img/image 的 HTML 保存到磁盘
【发布时间】:2013-05-06 06:53:36
【问题描述】:

我有一个 div,里面有一张图片。在 javascript 中,我得到了 div,并将 div 的 innerHTML 发送到服务器。下面的javascript过于简单:

function uploadImage() {       
    var imageInfo = document.getElementById('divImage').innerHTML;
    PageMethods.uploadImage(imageInfo, onSuccess, onError);

} //end function

我目前在服务器上接收这个作为字符串。

[WebMethod]
public static string uploadImage(string base64FileString){...}

结果如下:

<img height="150" width="150" title="name.png" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...../>

我需要将此图像保存到磁盘,但我不知所措。据我了解,我可以通过“base64”(使用拆分)获取所有内容并使用以下内容创建图像:

    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64FileString);
    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);
    image.Save(.....);

但这似乎非常低效。有没有更好的方法从字符串创建图像,更好的接收字符串的方法,或者更好的传递字符串的方法?

【问题讨论】:

    标签: c# javascript image upload base64


    【解决方案1】:

    你可以使用File.WriteAllBytes:

    byte[] imageBytes = Convert.FromBase64String(base64FileString);
    File.WriteAllBytes(path, imageBytes);
    

    一旦有了byte[],就完全不需要经过MemoryStreamSystem.Drawing 命名空间。

    【讨论】:

    • 看起来很干净。这需要某种拆分才能将 base64 部分从字符串中拉出,对吗?将字符串作为本身传递我抛出异常:“输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。”
    • @ter24 - 嗯,是的。 FromBase64String 只需要一个 base64 字符串,而不是序言。
    【解决方案2】:

    我知道必须有更好的方法来做到这一点,但我有一个至少可行的解决方案。如果有人能指出一种更复杂的方式,我会全力以赴。我会假设有某种对象会接收服务器上的 img 以保存到磁盘,但可能不是。

    Javascript 和 webmethod 保持不变。我将 div 的 InnerHTML 传递给服务器,并将其作为字符串接受。在服务器上,我使用了多个拆分(我理解这非常慢)来获取图像的 base64 部分。

    [WebMethod]
    public static string uploadImage(string base64FileString){
    
       //Get all of the text right of the comma
        string base64PartTemp= base64FileString.Split(',')[1];
    
        //The final part of the base64 had a \" to remove
        //Now base64PartFinal is the base64 part of the image only
        string base64PartFinal= base64PartTemp.Split('\"')[0];
    
        //Get all of the text to the right of the period from original string
        string fileTypePart = base64FileString.Split('.')[1];
    
        //Because the file is an image the file type will be 3 chars
        string fileType = fileTypePart.Substring(0, 3);
    
        //Use a new guid for the file name, and append the fileType
        string finalFileName = Guid.NewGuid() + "." + fileType;
    
        //Turn the final base64 part into byte array
        byte[] imageBytes = Convert.FromBase64String(base64PartFinal);
    
        //Get the working directory of the project to store the files
        string path= System.AppDomain.CurrentDomain.BaseDirectory.ToString();
    
        //Append that I want to put the image in the images folder, under a designated filename
        path += "Images/" + finalFileName;
    
        //Write the image to file
        File.WriteAllBytes(path, imageBytes);
    
    ...
    }
    

    这几天我都找不到答案。我希望它可以帮助某人。就像我说的,这可能不是最有效的解决方案,但它确实有效。

    【讨论】:

      猜你喜欢
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      相关资源
      最近更新 更多