Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

angular2 routing - Where to upload image in angular 2 project?

I have an app that can upload images. Everything is working fine, I;m not having issues with the upload itself... But I have issues because as soon as I upload the image and storage in the server, the angular-clie reload the page.

At first, I was uploading the images to /src/assets Then I relized that angular-clie is refreshing the page, because it detect some new content on the folder it's listeing. So I change that and now the images are being uploading to /resources (outside src folder) so now angular-clie is not refreshing the page... But they are not vissible for the web app as they are not in the "public folder".

So my question... Where should I upload the images without fire a page reload? / Or how I add an exception to angular-cli?

As backend I'm using Java with Spring, and that part is working fine. My angular2 structure is:

/
/resources
/src
/src/app
/src/assets
/dist

etc...

My angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
  "name": "ng2angle"

    },
    "apps": [
      {
        "root": "src",
        "outDir": "dist",
        "assets": [
          "assets",
          "favicon.ico"
        ],
        "index": "index.html",
        "main": "main.ts",
        "polyfills": "polyfills.ts",
        "test": "test.ts",
        "tsconfig": "tsconfig.app.json",
        "testTsconfig": "tsconfig.spec.json",
        "prefix": "app",
        "styles": [
          "app/core/preloader/preloader.scss",
          "styles.scss"
        ],
        "scripts": [
          "../node_modules/jquery/dist/jquery.js",
          "app/core/preloader/preloader.js"
        ],
        "environmentSource": "environments/environment.ts",
        "environments": {
          "dev": "environments/environment.ts",
          "prod": "environments/environment.prod.ts"
        }
      }
    ],
    "e2e": {
      "protractor": {
        "config": "./protractor.conf.js"
      }
    },
    "lint": [
      {
        "project": "src/tsconfig.app.json"
      },
      {
        "project": "src/tsconfig.spec.json"
      },
      {
        "project": "e2e/tsconfig.e2e.json"
      }
    ],
    "test": {
      "karma": {
        "config": "./karma.conf.js"
      }
    },
    "defaults": {
      "styleExt": "scss",
      "prefixInterfaces": false,
      "inline": {
        "style": false,
        "template": false
      },
      "spec": {
        "class": false,
        "component": true,
        "directive": true,
        "module": false,
        "pipe": true,
        "service": true
      }
    }
  }

COnsole Log when uploading: Angular is running in the development mode. Call enableProdMode() to enable the production mode.

{success: true, code: null, data: {…}}
[WDS] App updated. Recompiling...
[WDS] App updated. Reloading...

Function to sent file to backend:

save(event) {
    let fileList: FileList = event;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        let formData:FormData = new FormData();
        formData.append('file', file, file.name);
        let headers = new Headers();
        /** No need to include Content-Type in Angular 4 */
        let options = new RequestOptions(this.createAuthHeadersForUpload());
        return this._http.post(`${this.baseUrl + '/save'}`, formData, options)
            .map((response: Response) => <JsonResponse>response.json())
            .do(function (response) {
              console.log(response);
            })
            .catch(this.handleError);
      }
      else
        return null;
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, finally I couldn't make it works in my local environment, but I have to have a dev environment, where I can store the images and open them from there... Anyway, this is not happening in PRD as there I have an Apache server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...