Closed mariiahnied closed 3 years ago
Week 4 Step 2 ⬤⬤◯◯◯◯◯ | 🕐 Estimated completion: 20-25 minutes
Ideally, users will fill the web form with the correct information. However, people make mistakes. In this step, we will be implementing some form validation to make sure that a user has inputted text value before submitting their password.
bunnimage-frontend
branch multipart/form-data
in index.html
index.html
that accepts image/x-png,image/jpeg
and add the attribute name=image
getImage
event handler function in index.js
to create an alert if either the file input or the name
input is nulltype
attribute to "submit"
bunnimage/index.html
and bunnimage/script.js
Open up your LiveServer plugin and start your server.
By default, the website will reload whenever a user presses submit. This leads to our text on the page that we set being cleared away. We can prevent this by adding this to the top of our getImage()
function:
event.preventDefault()
In HTML Forms, the enctype
attribute specifies how the form-data should be encoded when submitting it to the server. Like we learned before, if we want to upload files, we need send form-data encoded as multipart/form-data
<h1>Bunnimage</h1>
<form onsubmit="getImage(event)" enctype="multipart/form-data">
<label>Code: </label>
To add the image upload input, add an additional file input within the form & change the submit button's type
.
<form onsubmit="getImage(event)" enctype="multipart/form-data">
<input type="file" name="image"></input>
<input id="name" type="text" placeholder="Enter your file's name">
<input type="submit" />
We need to validate two things.
Week 4 Step 3 ⬤⬤⬤◯◯◯◯ | 🕐 Estimated completion: 20-25 minutes
fetch
HappenWe are going to how use the form to call our Azure Function! Yay :tada:! Things are coming together.
useImage()
function with await fetch
to send the form input to your Bunnimage Azure Functionoutput
div to Your image has been stored successfully!
bunnimage/script.js
to the bunnimage-frontend
branchOpen up your LiveServer plugin and start your local server. To test your web app:
:one: Upload an image and click submit. Do you get a "Your image has been stored successfully!" message?
:two: Log into your Azure portal and navigate to your storage account. In your container, do you see your image?
To get the information the user entered in the form, you will need to create a FormData
object. FormData lets you compile a set of key/value pairs to send.
var bunniForm = document.getElementById("YOUR_FORM_ID");
var payload = new FormData(bunniForm);
const file = fileInput.files[0]; // fileInput is the file upload input element
payload.append("file", file);
The Fetch API is built in to Javascript to make HTTP requests (using GET, POST and other methods), download, and upload files.
:bulb: We don't have to install
node-fetch
as an npm package sincefetch
is built into JavaScript. Remember: we were using NodeJS before.
To start a request, call the special function fetch()
:
const response = await fetch(resource, options);
Fetch
accepts two arguments:
resource
: the URL string, or a Request objectoptions
: the configuration object with properties like method, headers, body, credentials, and more.In order to pass the type of method, the payload in the pody, and headers, you have to configure the "options" part of the fetch function.
const response = await fetch(resource, {
method: ... ,
body: ... ,
headers: { ... }
});
Fill in the method, headers and body with the correct information compatible with your API!
:bulb: Refer back to previous Fetch requests we made in NodeJS for hints. Remember for this request, we are not receiving JSON!
We need to let the users know if the upload was successful! We do this by making sure we receive a response from our upload API:
CORS (Cross-origin resource sharing) settings tells the Function App what domains (such as wwww.website.com) can make requests to the Azure Functions.
Week 4 Step 4 ⬤⬤⬤⬤◯◯◯ | 🕐 Estimated completion: 5-20 minutes
Write and connect a JS file to make a GET request to the download Azure function that includes the codename to receive the link. Edit the button on the HTML page so that it downloads the file when clicked.
bunnimage-frontend
branchid
with the value button1
onto the upload buttonid
of downloadusername
and create a button with an id
of button2
that calls the function downloadImage()
in index.html
downloadImage()
function with a await fetch
GET request to send downloadusername
to your Bunnimage Azure Function to return the file download uriwindow.open()
and make sure it replaces the page instead of opening a new tab.bunnimage/index.html
and bunnimage/script.js
Open up your LiveServer plugin and start your local server. To test your web app:
⚠️ Type in the username for an image you have uploaded previously. Does the download button link to your file?
window.open()
is fairly straightforward to use. Here's the syntax:
window.open(URL, name, specs, replace)
We will only be using URL
and name
. Use this documentation to fill it in.
Go ahead and merge this branch to main
to move on. Great work finishing this section!
⚠️ If you receive a
Conflicts
error, simply pressResolve conflicts
and you should be good to merge!
Changes made
Closes #[issue number]