简体   繁体   English

将base64图像转换为javascript中的文件或如何使用jquery ajax传递大的base64字符串

[英]Convert base64 image to file in javascript or how to pass a big base64 string with jquery ajax

This can't take any more of my time. 我不能再花时间了。 I've tried to solve this a very long time now. 我已经尝试解决很长时间了。

I will give you my whole scenario and then what the problem is. 我会给你我的整个情况,然后是什么问题。

I have this web site. 我有这个网站。 On one page the user can choose between three image input types. 在一页上,用户可以在三种图像输入类型之间进行选择。

This is a radio button group: 这是一个单选按钮组:

o Twitter Logo
o Twitter Profile Picture
o Upload picture

If the user choose option 1 or 2, an img tag src is updated with a local-project file ( http://localhost:9000/public/images/image.png ) and this image src is stored in html5 Web Session Storage variable. 如果用户选择选项1或2,则会使用本地项目文件( http://localhost:9000/public/images/image.png )更新img标签src并将此图像src存储在html5 Web会话存储变量中。

If the user choose option 3 he/she get to choose a file from their computer (a input type="file" appears under the radio group) and the img tag src is updated with this image. 如果用户选择选项3,则他/她可以从他们的计算机中选择一个文件(输入type =“ file”出现在单选组下),并且img标签src使用该图像更新。 This time, the src that I will store in the session variable won't be a path to the file (which I know is because of security reasons) but the src will be a base64 string. 这次,我将存储在会话变量中的src不会是文件的路径(我知道这是出于安全原因),但是src将会是base64字符串。 A really big one if the user choose a big image. 如果用户选择大图像,那将是一个很大的图像。

So now I have this image stored in the session variable, either a path to the image file included in the project folder or a base64 encoded image. 因此,现在我将此图像存储在会话变量中,该变量可以是项目文件夹中包含的图像文件的路径,也可以是base64编码的图像。

What I do now is to fetch this value from the session variable in JavaScript. 我现在要做的是从JavaScript中的会话变量中获取此值。 I want to pass this image to my code on the server side. 我想将此图像传递到服务器端的代码中。 For making an actual image of it and uploading it to places, but that part isnt really necessary. 对于制作它的实际图像并将其上传到地方,但这部分并不是真正必要的。

My problem is that in JavaScript, I can't pass this with a POST using $.ajax . 我的问题是在JavaScript中,无法使用$.ajax在POST中传递它。 The base64 string is too big I think, and I can't figure out how I can convert it to something else, say a byte[]. 我认为base64字符串太大,无法弄清楚如何将其转换为其他内容,例如byte []。

How should I do? 我应该怎么做? I want to pass this image that the user choose to the server side for further process. 我想将此用户选择的图像传递给服务器端,以进行进一步处理。 Then on the server side I want to convert it to an actual Image object, or BufferedImage. 然后在服务器端,我想将其转换为实际的Image对象或BufferedImage。

Here's a code-block of how it looks now: 这是现在的外观的代码块:

function gatherSessionValuesAndGenerateCode(userEmail) {
var email           = userEmail;
var category        = getSessionValue("category");
var itemName        = getSessionValue("itemName");
var service         = getSessionValue("service");
var accountName     = getSessionValue("accountName");
var action          = getSessionValue("action");
var imageType       = getSessionValue("imageType");
var imageFile       = getSessionValue("imageFile");
var expirationDate  = getSessionValue("expirationDate");



$.ajax({
    type: "POST",
    url: "http://localhost:9000/quikkly/business/create/generate",
    data: {
        email: email,
        category: category,
        itemName: itemName,
        service: service,
        accountName: accountName,
        action: action,
        imageType: imageType,
        imageFile: imageFile, //This is making me feel ill, don't know how to solve it.
        expirationDate: expirationDate

    },
    success: function(response){console.log("Horayyy "+response)}
});
}

ok i got your point but what i am trying to say is may be your string contains 20K characters but i don't think it can be larger than 2-3 Mb and i hope your server settings allows you to post data of size of 2-3 MB. 好的,我明白了,但是我想说的是您的字符串可能包含20K个字符,但我认为它不能大于2-3 Mb,我希望您的服务器设置允许您发布大小为2的数据-3 MB。

Apart from this i think putting the image path name in base_64 is not a good idea.If you think someone can steel the data from your seesion then he/she can do any thing with your complete website concept. 除此之外,我认为将图像路径名放在base_64中不是一个好主意。如果您认为某人可以从您的视野中窃取数据,则他/她可以使用您完整的网站概念来做任何事情。

Because any how a person can see the image path even when you display some image over web page. 因为即使您在网页上显示某些图像,任何人都可以看到图像路径。

Still if you think you don't want path in seesion you can keep it in some file in key-value pair or you can keep it in database. 仍然,如果您认为不希望出现路径,则可以将其保存在键值对的某个文件中,也可以将其保存在数据库中。

OR you can do one more thing just keep the image file name in your seesion data & prepend the exact path when you want to use it internally over server side. 或者,您还可以做另一件事,就是只将图像文件名保留在查看数据中,并在要在服务器端内部使用它时在路径之前加上确切的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM