0

I don't understand the below code

const req = new HttpRequest('POST', '/upload/file', file, {
  reportProgress: true
});

I need clarity on the second argument which is a URL, at the moment I do not understand how it is supposed to map to server. I do realize that the above code doesn't use http, and. That's where I get lost. Please explain how server will see this request without URL.

1 Answer 1

1

The code won't work if you don't specify the url. Typically springboot works in 8080 port in local machine. Following is a sample code of controller.

@RestController
@RequestMapping("/upload")
public class ExampleClass{

    @PostMapping("/file")
    public String fileUpload(MultipartFile file){
        // upload file code
    }

}

@RequestMapping("/upload") is a parent mapping of whole controller, and @PostMapping("/file") is a child mapping. (It depends if you need @PostMapping("/upload/file") you can have it). This is the path you need to send the request to server from the front-end. So server will identity that this is the post request coming, so it will execute the method.

if you server port is running in 8080, you need to change to 'localhost:8080/upload/file' instead of /upload/file

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

3 Comments

But when I send the file, spring says Current request is not a multi part request... Do you have any idea?
Uploaded successfully, it's cool now, I removed content type. Thanks a lot for your time. We'll i didn't follow Bezkoder, our codes are similar anyway...

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.