banner



How To Add A File Upload To My Website

Setting up a basic file upload feature for your static website with just Javascript using Firebase. No PHP needed!

Click the button on the correct.

Click on storage, then cull the Rules tab
          service firebase.storage {                      match /b/{bucket}/o {                      match /{allPaths=**} {                      allow read, write: if request.auth != nil;                      }
}
}
          service firebase.storage {                      friction match /b/{bucket}/o {                      lucifer /{allPaths=**} {                      let read, write;                      }
}
}
          <div id="filesubmit">
<input blazon="file" class="file-select" accept="image/*"/>
<push class="file-submit">SUBMIT</button>
</div>
          const storageService = firebase.storage();
const storageRef = storageService.ref();
          certificate.querySelector('.file-select).addEventListener('modify', handleFileUploadChange);          document.querySelector('.file-submit).addEventListener('click', handleFileUploadSubmit);        
          let selectedFile;
handleFileUploadChange(eastward) {
selectedFile = e.target.files[0];
}
          handleFileUploadSubmit(e) {                      const uploadTask = storageRef.kid(`images/${selectedFile.name}`).put(selectedFile); //create a child directory called images, and identify the file inside this directory                      uploadTask.on('state_changed', (snapshot) => {
// Find state modify events such as progress, break, and resume
}, (error) => {
// Handle unsuccessful uploads
console.log(fault);
}, () => {
// Practice something one time upload is consummate
console.log('success');
});
}
  • Open terminal, and type cd in the command prompt (without hitting enter)
  • Open up finder, and become to the root binder containing your projection'south html, and elevate information technology into the terminal window next to the cd you lot just typed, then hit return.
  • Blazon python -m SimpleHTTPServer 8000
  • Open a web browser and enter the URL: http://localhost:8000. You should see your form folio containing the file upload field and submit button, and those buttons should allow you to select a file for upload, and submit it to your Firebase project database. We can cheque the debug panel to make sure we run into a success bulletin:

Checking for our success message in the debug console in Chrome

  • Hibernate the submit push until the outset change event fires, so that nosotros can make sure we take a file that we can upload first earlier allowing submission..
  • Add actual error handling and success messaging so the user knows what's happening.
  • Store the image metadata as well
  • Make certain users are authenticated earlier uploading
  • Make sure files tin can just be uploaded from specific domains:

Source: https://mheavers.medium.com/setting-up-a-basic-file-upload-feature-for-your-static-website-with-just-javascript-using-firebase-32464580d8bb

Posted by: riveratrustion.blogspot.com

0 Response to "How To Add A File Upload To My Website"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel