How to Upload a file using angular4 for browsing and api created in golang?

350 views
Skip to first unread message

Mandeep Kaur

unread,
Nov 9, 2017, 10:36:59 PM11/9/17
to golang-nuts
Hi,

I am working on an application in which I need to upload a file using Golang and Angular 4.Suppose I have an input type file and an upload button on the screen. When I browse a file from my system and clicks upload button.Now following are my queries regarding file upload:

1. How will angular process the file at front end?

2. If it processes the file then what it will return to the rest api as data.

3. Which method will be used to process the file like if it is an image then which methods like XHR, HTTP2.0 and Base64encode will be used?

4. Can file be uploaded via angular script and it will give download url to rest api?

5. The uploaded file should not be executable.

6. The file should only be rendered using that download url.Hope I have framed the queries well. If there is anything missing please add to the question.

Thanks!

Marcus Franke

unread,
Nov 11, 2017, 10:17:12 AM11/11/17
to Mandeep Kaur, golang-nuts


I am working on an application in which I need to upload a file using Golang and Angular 4.Suppose I have an input type file and an upload button on the screen. When I browse a file from my system and clicks upload button.Now following are my queries regarding file upload:

1. How will angular process the file at front end?

Why should your angular frontend process the file at all?

2. If it processes the file then what it will return to the rest api as data.

The easiest way, I guess, will be a simple form based upload. The file will be uploaded to the endpoint you specify. Thats a POST action.

The handler responsible for the upload can copy the request body in a file object and you are done.

```
*http.Request
file, handler, err := r.FormFile("fileupload")
f, err := os.OpenFile(somePath+"/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0644)
_, err = io.Copy(f, file)
```

4. Can file be uploaded via angular script and it will give download url to rest api?

Its upon your code what you write into the response. Of course could you return the file location where you did save the file.

5. The uploaded file should not be executable.

Look at the flags I set in os.Open(). There you control if a file will be executable or not. But that is only relevant for the server you save your files.



Reply all
Reply to author
Forward
0 new messages