我通过FormData将一个文件从Angular 5.xx上传到Jersey 1.xx.
I upload one file from Angular 5.xx to Jersey 1.xx via FormData.
数据已收到并成功保存在我的服务器应用程序目录中,但是浏览器在图片中显示了这一行(Chrome和Firefox).
The data gets received and saved successfully on my server app directory, but Browser says this line from the picture (Chrome and Firefox).
当我仅通过HTML上传时,就像这样:
When I upload it via HTML only like so:
Choose file to upload<br> <form action="localhost:8181/BackendMaven" method="post" enctype="multipart/form-data"> <input name="input" id="filename" type="file" /><br><br> <button name="submit" type="submit">Upload</button> </form>它不会显示.
以下是角度的FromData代码:
Here is the angular FromData Code:
@ViewChild('fileInput') fileInput; submitFile(): void{ console.log("submitFIle called!!!"); let fi = this.fileInput.nativeElement; let fileToUpload = fi.files[0]; let formData = new FormData();//empty formdata formData.append("input", fileToUpload); console.log(formData.get("input")); this.http.post(this.URI_UPLOAD,formData).subscribe();}和服务器端(即使用tomcat)
And Server Side (im using tomcat)
public Response uploadFile(@FormDataParam("input") InputStream uploadedInputStream, @FormDataParam("input") FormDataContentDisposition fileDetail) { ... return Response.status(200).entity("Successfully uploaded to location: " + FileFactory.getFilePath(uploadedFileLocation)).build(); } }推荐答案
尝试实际发送JSON响应,或告诉angular期望有所不同.如果您使用的是 HttpClient ,则默认情况下将使用JSON.
Try to actually send a JSON response, or tell angular to expect something different. If you're using HttpClient, it will by default expect JSON.
类似这样的东西:
this.http.post(url, body, { responseType: 'text' }).subscribe();更多推荐
成功在Angular中执行POST后的HttpErrorResponse 200
发布评论