Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7111cbb

Browse files
Added option to upload images
1 parent 1dde9eb commit 7111cbb

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

‎functions/save.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,29 @@ exports.handler = function(event, context, callback) {
2323
secretAccessKey: process.env.S3_SECRET,
2424
});
2525

26-
// Add a file to a Space
27-
var params = {
28-
Body: JSON.stringify(jsondata.data),
29-
Bucket: "netlify-spaces",
30-
Key: "data.json",
31-
ContentType: "application/json",
32-
ACL: "public-read",
33-
};
26+
if (jsondata.type == "json") {
27+
let filename = "data.json";
28+
var params = {
29+
Body: JSON.stringify(jsondata.data),
30+
Bucket: "netlify-spaces",
31+
Key: filename,
32+
ContentType: "application/json",
33+
ACL: "public-read",
34+
};
35+
} else {
36+
let filename =
37+
"img/" +
38+
Math.random().toString(36).substring(2, 15) +
39+
Math.random().toString(36).substring(2, 15) +
40+
".jpg";
41+
var params = {
42+
Body: JSON.stringify(jsondata.data),
43+
Bucket: "netlify-spaces",
44+
Key: filename,
45+
ContentType: "image/jpeg",
46+
ACL: "public-read",
47+
};
48+
}
3449

3550
s3.putObject(params, function(err, data) {
3651
if (err) console.log(err, err.stack);
@@ -39,7 +54,7 @@ exports.handler = function(event, context, callback) {
3954

4055
callback(null, {
4156
statusCode: 200,
42-
body: JSON.stringify({ foo: "bar" }),
57+
body: JSON.stringify({ filename: filename }),
4358
});
4459
} catch (error) {
4560
return {

‎public/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</head>
1313

1414
<body>
15-
<div class="container text-center">
15+
<div class="container text-center mt-5">
1616
<button class="btn btn-outline-dark" id="save" style="display: none;">
1717
Save
1818
</button>
@@ -35,7 +35,7 @@
3535
let user = await netlifyIdentity.currentUser();
3636

3737
let data = {
38-
title: "hello world",
38+
title: "Hello world. This is some text.",
3939
};
4040

4141
const response = await fetch("/.netlify/functions/save", {
@@ -45,6 +45,7 @@
4545
method: "post",
4646
body: JSON.stringify({
4747
data: data,
48+
type: "json",
4849
}),
4950
}).then(function(res) {
5051
document.querySelector("#result").innerHTML = "Succes!";

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /