Uploading & Downloading an image as a byte array
You're not converting it to base64, that's the problem. You're telling it it's a base64 string but it's not. Instead of just using e.target.result you need to first encode it in base64, e.g.
btoa(e.target.result)
https://jsfiddle.net/drkz2fth/
Play Framework download image from byte array
I personally would need some more information to debug this, however, perhaps you can gain the information you need to continue based on an example of how I would store and retrieve images in PostgreSQL using Ebean.
The model would look something like this:
public class User extends Model {
public static Finder<LInteger, User> find = new Finder<Integer,User>(User.class);
@Id
public Integer id;
public String name;
@Constraints.Required
@Constraints.Email
public String email;
public String password;
@Lob
public byte[] photo;
public static User findByEmail(String email) {
return find.where().eq("email", email).findUnique();
}
}
The controller would look something like this:
public class Application extends Controller {
public static Result userPhoto(Integer userId) {
User user = User.find.ById(userId);
if (user == null) {
return notFound();
}
if (user.photo == null) {
return ok(Play.application().getFile("/public/images/default-user.jpg"), true).as("image/jpeg");
}
return ok(new ByteArrayInputStream(user.photo)).as("image/jpeg");
}
}
I hope this helps!
How do I display a byte array image from a backend that uses AWS S3, in a React component?
This approach downloads the image, gets the byte array, converts it to a Blob
and stuffs the binary data into a magic url you can use for the image source.
const IMAGE_URL = "https://placekitten.com/200/300";
const fetchImage = async (url) => {
try {
const response = await fetch(url);
const imageBytes = await response.arrayBuffer();
var blob = new Blob([imageBytes], { type: "image/jpeg" });
var imageUrl = URL.createObjectURL(blob);
return imageUrl;
} catch (error) {
console.log("ERROR:", error);
}
};
(async () => {
const imageSrc = await fetchImage(IMAGE_URL);
console.log(imageSrc);
document.getElementById("kittenImage").src = imageSrc;
})();
document.getElementById("app").innerHTML = `
<h1>Kitty!</h1>
<image id='kittenImage'>
`;
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<script src="src/index.js">
</script>
</body>
</html>
Related Topics
Firebase Custom Event Parameters Not Visible in Console
Launch an App from Within Another (Iphone)
Fast and Lean Pdf Viewer For Iphone/Ipad/Ios - Tips and Hints
Sending an Http Post Request on Ios
Swift 2.0 - Binary Operator "|" Cannot Be Applied to Two Uiusernotificationtype Operands
How to Insert New Cell into Uitableview in Swift
How to Download Multiple Files Sequentially Using Nsurlsession Downloadtask in Swift
How to Create a Custom Pop Up View With Swift
Transport Security Has Blocked a Cleartext Http
Dateformatter Doesn't Return Date For "Hh:Mm:Ss"
How to Present Uialertcontroller When Not in a View Controller
How to Add Nsapptransportsecurity to My Info.Plist File
How Does a Delegate Work in Objective-C
Eliminate Extra Separators Below Uitableview
Change Paid App to Free But Know If User Previously Purchased It