首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#将base64转换为jpg图像

c#将base64转换为jpg图像
EN

Stack Overflow用户
提问于 2016-11-15 03:38:28
回答 2查看 2.5K关注 0票数 0
代码语言:javascript
复制
  protected void ExportToImage(object sender, EventArgs e)
    {
        string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
        byte[] bytes = Convert.FromBase64String(base64);
        File.Copy(bytes.ToString()+".jpg", "\\\\192.168.2.9\\Web");
    }

我从jquery获得的hfImageData数据,在用户完成绘图之后,我将把卷积转换成base64。然后调用c#函数将base64转换成jpg图像文件到服务器,并存储到DB中。

当我触发onclick按钮im获取错误时,:System.IO.FileNotFoundException:找不到文件‘System.Byte[]..jpg’.

知道为什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-15 03:46:55

在这一行File.Copy(bytes.ToString()+".jpg", "\\\\192.168.2.9\\Web");中,您实际上是在尝试转换字节数组的内容并将其用作图像名称,但这实际上不会创建文件。

bytes.ToString()只是返回对象的类型,而不是内容。所以你才会看到System.Byte[].jpg

解决这个问题的方法是改变您的功能:

代码语言:javascript
复制
protected void ExportToImage(object sender, EventArgs e)
{
    string base64 = Request.Form[hfImageData.UniqueID].Split(',')[1];
    byte[] bytes = Convert.FromBase64String(base64);
    //write the bytes to file:
    File.WriteAllBytes(@"c:\temp\somefile.jpg", bytes); //write to a temp location.
    File.Copy(@"c:\temp\somefile.jpg", @"\\192.168.2.9\Web");//here we grab the file and copy it.
    //EDIT: based on permissions you might be able to write directly to the share instead of a temp folder first.
}
票数 2
EN

Stack Overflow用户

发布于 2016-11-15 03:42:17

bytes.ToString()不返回任何有意义的内容。

您的字节不是文件;您不能复制它们。

相反,调用File.WriteAllBytes()将它们直接写入一个新文件。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40601591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档