2

I want to upload an image in Angular and save it under Folder Assets (without using API)

4
  • 4
    Writing to a server without API is not possible with best of my knowledge Commented Jun 28, 2019 at 14:19
  • 1
    You've to write server-side code. Angular is client side, you cannot upload files directly... Commented Jun 28, 2019 at 14:26
  • You can use Firebase if you do not want write back-end Commented Jun 28, 2019 at 14:26
  • 1
    not possible without API Angular is client side Commented Jun 28, 2019 at 14:27

3 Answers 3

1

You can sort of do this if you're using a static file host with a client SDK (such as AWS S3).

Assume you're hosting out of an s3 bucket named mysite.com, and you've configured this buckets assets folder to allow public uploads (maybe a bad idea...)

in your project, you've run npm install aws-sdk

and you have some aws upload service like

import {Injectable} from '@angular/core';
import * as AWS from 'aws-sdk';

const BUCKET = 'mysite.com';

@Injectable()
export class AwsUploadService {

   private s3 = new AWS.S3({
     apiVersion: '2006-03-01',
     params: {Bucket: BUCKET}
   });

   uploadToAssets(file: File) {
      this.s3.upload({
        Key: 'assets/' + file.name,
        Body: file
      }, (err, data) => {
         console.log(err, data); // this will tell you if it went ok or not
      });
   }
}

then you can just call that upload function as needed with the file you want to upload, but again, this is probably a bad idea, and you're much better off having a server. If you're not using a static file host, then there really isn't much you can do without writing a server.

here's more about using s3 in the browser: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

Sign up to request clarification or add additional context in comments.

Comments

1

Uploading an image without API would be static.

Comments

0

Angular is a client side code. That is it when a request is made to access an Angular app, it is download to the client Machine and runs there. If you make any changes, it is only available in your browser session. Once you close and open again i.e. Again opened making a new request you get the app as it was when you opened. Therefore to persist any changes you have to send it to server and for that you need a server side application (API).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.