System.Convert.ToBase64String返回与System.Convert.FromBase64String的输入不同的结果(System.Convert.ToBase64Strin

编程入门 行业动态 更新时间:2024-10-09 05:22:12
System.Convert.ToBase64String返回与System.Convert.FromBase64String的输入不同的结果(System.Convert.ToBase64String returning different result than the input to System.Convert.FromBase64String)

我正在编写一个Web应用程序,可以将图像从Web保存到数据库中。 目前,我用来保存图像的方法如下:

[WebMethod] public static void Save(string pictures) { // pictures is an object containing the URL of the image along with some metadata List<ImageObject> imageList = new JavaScriptSerializer().Deserialize<List<ImageObject>>(pictures); for (int i = 0; i < imageList.Count; i++) { var webClient = new WebClient(); byte[] byteArray = webClient.DownloadData(imageList[i].URL); imageList[i].Picture = byteArray; } // some SQL to save imageList }

ImageObject:

[DataContract] public class ImageObject { [DataMember] public string URL { get; set; } [DataMember] public string Picture { get; set; } }

现在,这适用于从URL下载。 保存到我的数据库的条目:

0xFFD8FFE000104A464946000101[...]331B7CCA7F0AD3A02DC8898F

要显示此图像,我只需在检索后调用System.Convert.ToBase64String()并将其用作图像src 。 但是,我现在正在尝试为用户添加一些功能来上传自己的图片。 我的函数,从<input />调用:

function uploadPicture(){ var numPics = document.getElementById("uploadedPictures").files.length; var oFReader = new FileReader(); oFReader.onload = function (oFREvent) { var src = oFREvent.target.result; document.getElementById('image').src = src; } oFReader.readAsDataURL(document.getElementById("uploadedPictures").files.length; }

使用它时,这是我从浏览器获得的图像源(当原始图像仅为~500 KB时超过2 MB):

data:image/bmp;base64,/9j/4AAQSkZJRgABAgEAY[...]k8tTMZEBCsEbSeRYem493IHAfiHWq6vKlOv/Z

当用作源时,它会正确显示。 但是,由于这不是一个实际的URL(并且WebClient.DownloadData()仅限于<260个字符的URL),我一直在尝试其他方法将这些数据保存到我的数据库中。 但是,我似乎找不到一个函数来保存它与WebClient.DownloadData()相同的格式。 例如,我已经看过将base 64字符串转换为图像并保存它 ,其中大多数答案似乎使用Convert.FromBase64String() ,但使用它似乎以不同的格式保存到我的数据库:

0x64006100740061003A0069006D0[...]10051004500420041005100450042004

试图在此返回时使用System.Convert.ToBase64String()

ZABhAHQAYQA6AGkAbQBhAGcAZQAvAGIAbQBwADsAYgBh[...]UwBlAFIAWQBlAG0ANAA5ADMASQBIAEEAZgBpAEgAVwBxADYAdgBLAGwATwB2AC8AWgA=

这与我在Convert.FromBase64String()上使用的不同。 我在谷歌上发现的其他东西,试图让它以相同的格式保存或显示为图像,到目前为止还没有成功。

因此,我想知道是否存在将结果从FileReader转换为与WebClient.DownloadData()相同的格式的方法0x6400[...] WebClient.DownloadData()对于URL或者如果有某种方法将0x6400[...]数据转换为格式可以通过将其用作<img>源来显示。

I'm writing a web application which saves images from the web to a database. Currently, what I use to save images is as follows:

[WebMethod] public static void Save(string pictures) { // pictures is an object containing the URL of the image along with some metadata List<ImageObject> imageList = new JavaScriptSerializer().Deserialize<List<ImageObject>>(pictures); for (int i = 0; i < imageList.Count; i++) { var webClient = new WebClient(); byte[] byteArray = webClient.DownloadData(imageList[i].URL); imageList[i].Picture = byteArray; } // some SQL to save imageList }

ImageObject:

[DataContract] public class ImageObject { [DataMember] public string URL { get; set; } [DataMember] public string Picture { get; set; } }

Now, this works just fine for downloading from a URL. The entry saved to my database:

0xFFD8FFE000104A464946000101[...]331B7CCA7F0AD3A02DC8898F

To display this image, I simply callSystem.Convert.ToBase64String()after retrieving it and use it as an imagesrc. However, I am now trying to add some functionality for users to upload their own pictures. My function for this, called from an<input />:

function uploadPicture(){ var numPics = document.getElementById("uploadedPictures").files.length; var oFReader = new FileReader(); oFReader.onload = function (oFREvent) { var src = oFREvent.target.result; document.getElementById('image').src = src; } oFReader.readAsDataURL(document.getElementById("uploadedPictures").files.length; }

When using this, this is the image source I get from the browser (over 2 MB when the original is only ~500 KB):

data:image/bmp;base64,/9j/4AAQSkZJRgABAgEAY[...]k8tTMZEBCsEbSeRYem493IHAfiHWq6vKlOv/Z

This gets displayed properly when used as a source. However, since this is not an actual URL (andWebClient.DownloadData()is restricted to URLs of < 260 characters), I have been trying other methods to save this data into my database. However, I cannot seem to find a function to save it in the same format asWebClient.DownloadData(). For example, I have looked at converting a base 64 string to an image and saving it, where most of the answers seem to useConvert.FromBase64String(), but using this appears to save in a different format to my database:

0x64006100740061003A0069006D0[...]10051004500420041005100450042004

Trying to useSystem.Convert.ToBase64String()on this returns

ZABhAHQAYQA6AGkAbQBhAGcAZQAvAGIAbQBwADsAYgBh[...]UwBlAFIAWQBlAG0ANAA5ADMASQBIAEEAZgBpAEgAVwBxADYAdgBLAGwATwB2AC8AWgA=

which is different from what I usedConvert.FromBase64String()on. Other things I found on Google to try and get it to save in the same format or display as an image have not worked thus far.

Hence, I am wondering whether there exists a method to convert the result from aFileReaderto the same format asWebClient.DownloadData()does for URLs or if there is some way to convert the0x6400[...]data to a format that can be displayed by using it as an<img>source.

最满意答案

事实证明,数据保存到数据库的原因始于0x64006100[....]而不是0xFFD8FFE0[...]是因为我忘记删除初始data:image;base64,的URI data:image;base64, . 执行此操作后,使用System.Convert.ToBase64String()可以保存并正确读取所有内容。

It turns out that the reason the data saved into the database began with0x64006100[....]instead of0xFFD8FFE0[...]was due to me forgetting to strip the URI of the initialdata:image;base64,. After doing this, everything saves and is read properly usingSystem.Convert.ToBase64String().

更多推荐

本文发布于:2023-08-03 18:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398089.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ToBase64String   Convert   System   FromBase64String   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!