html代码
<label>
welcome {{name}}, welcome to new app.
</label>
<div>
<input type="file" multiple placeholder="Select Files to be upload" accept=".xlsx" (change)=selectedfiles($event)>
</div>
用于遍历文件列表并一次上载一个文件列表的上载逻辑
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import {UploadserviceService} from '../uploadservice.service';
import { HttpResponse, HttpEventType } from '@angular/common/http';
@Component({
selector: 'app-welcome',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {
name='';
selectedxlfiles:FileList;
url=environment.url;
result:any;
currentfile:File;
fileandinstancekeyobj:any={};
constructor(private router:ActivatedRoute,private http: HttpClient,private uploadservice:UploadserviceService) { }
ngOnInit(): void {
this.name=this.router.snapshot.params['name'];
}
selectedfiles(event){
this.selectedxlfiles=event.target.files;
for(let i=0;i<this.selectedxlfiles.length;i++){
console.log(this.selectedxlfiles[i]);
this.currentfile=this.selectedxlfiles[i];
this.uploadservice.uploadtoserver(this.currentfile).subscribe((res:any)=>{
console.log(res.body);
});
}
}
}
一次上载一个文件的Servicecall逻辑
import { Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class UploadserviceService {
constructor(private http:HttpClient) { }
uploadtoserver(selectedfile:File): Observable<HttpEvent<{}>>{
let url:string=environment.url+'uploadfile';
console.log(url);
const data: FormData=new FormData();
data.append('selectedfile', selectedfile);
const newrequest=new HttpRequest('POST',url,data,{
reportProgress: true,
responseType: 'text'
});
return this.http.request(newrequest);
//return this.http.post(url,selectedfiles);
}
}
package com.example.demo;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class uploadController {
Map<String, MultipartFile> filemap=new HashMap<String, MultipartFile>();
@PostMapping("/uploadfile")
public ResponseEntity<String> handlefileupload(@RequestParam("selectedfile") MultipartFile multifile){
String message="";
uploaddto dto=new uploaddto();
try {
message="succesfull";
System.out.println(multifile.getOriginalFilename());
return ResponseEntity.status(HttpStatus.OK).body(message);
} catch (Exception e) {
e.printStackTrace();
message="failed";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(message);
}
}
}
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment';
import {UploadserviceService} from '../uploadservice.service';
import { HttpResponse, HttpEventType } from '@angular/common/http';
@Component({
selector: 'app-welcome',
templateUrl: './welcome.component.html',
styleUrls: ['./welcome.component.css']
})
export class WelcomeComponent implements OnInit {
name='';
selectedxlfiles:FileList;
url=environment.url;
result:any;
currentfile:File;
constructor(private router:ActivatedRoute,private http: HttpClient,private uploadservice:UploadserviceService) { }
ngOnInit(): void {
this.name=this.router.snapshot.params['name'];
}
selectedfiles(event){
this.selectedxlfiles=event.target.files;
this.uploadservice.uploadtoserver(this.selectedxlfiles).subscribe((res:any)=>{
console.log(res.body);
});
}
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class UploadserviceService {
constructor(private http:HttpClient) { }
uploadtoserver(selectedfiles:FileList): Observable<HttpEvent<{}>>{
let url:string=environment.url+'uploadfile';
console.log(url);
const data: FormData=new FormData();
data.append('selectedfiles', selectedfiles);
const newrequest=new HttpRequest('POST',url,data,{
reportProgress: true,
responseType: 'text'
});
return this.http.request(newrequest);
}
}
package com.example.demo;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class uploadController {
Map<String, MultipartFile> filemap=new HashMap<String, MultipartFile>();
@PostMapping("/uploadfile")
public ResponseEntity<String> handlefileupload(@RequestParam("selectedfiles") MultipartFile[] multifile){
String message="";
try {
message="succesfull";
for(int i=0;i<multifile.length;i++){
System.out.println(multifile[i].getOriginalFilename());
}
return ResponseEntity.status(HttpStatus.OK).body(message);
} catch (Exception e) {
e.printStackTrace();
message="failed";
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(message);
}
}
}
2020-09-21 00:38:24.114错误11348---[nio-5200-exec-5]O.a.C.C.C.[.[.[/].[dispatcherServlet]:路径为[]的上下文中servlet[dispatcherServlet]的servlet.Service()引发异常[请求处理失败;嵌套异常为org.springframework.web.multipart.multipartException:当前请求不是多部分请求],具有根本原因
错误
尝试在controller中获取multipartfile[]
参数,但请求不是多部分请求。因此得到以下错误。
当前请求不是多部分请求
uploadtoserver(selectedfiles:FileList): Observable<HttpEvent<{}>>{
let url:string=environment.url+'uploadfile';
const data: FormData=new FormData();
for(let i=0;i<selectedfiles.length;i++){
data.append('selectedfiles', selectedfiles[i]);
}
const newrequest=new HttpRequest('POST',url,data,{
reportProgress: true,
responseType: 'text'
});
return this.http.request(newrequest);
}
我试图开始一个项目与Angular,但我不能进一步因为错误: “在multi./src/styles.css./node_modules/bootstrap/dist/css/bootstrap.min.css模块中找不到错误:” 依赖关系如下: json(相关部分): devDependencies: 我已经尝试了stackoverflow的所有解决方案,但都没有效果。我试过: 我尝试过组合不同
本文向大家介绍Angular 2 在应用程序引导中使用Guard,包括了Angular 2 在应用程序引导中使用Guard的使用技巧和注意事项,需要的朋友参考一下 示例 文件main.ts(或boot.ts) 考虑上面的示例: 创建防护(创建防护的位置)并 将Guard添加到路由配置中(将Guard配置为路由,然后导出APP_ROUTER_PROVIDERS), 我们可以将引导程序耦合到Guard
更新:(2020年5月18日)本帖末尾的解决方案! 我正在尝试将大型CSV文件(30MB-2GB)从浏览器上载到运行Python3.7Flask的GCP应用程序引擎,然后将这些文件推送到GCP存储。这在大型文件的本地测试中效果很好,但如果文件大小超过20MB,则在GCP上会立即出现错误,出现“413-您的客户端发出了太大的请求”。这个错误在上传时立即发生,甚至在它到达我的自定义Python逻辑之前
我正在尝试使用CSS来集中div“status”。已经尝试使用垂直-align:middle,display:flex,align-item:center,但都不起作用。有人能帮帮我吗?它看起来像是div的高度保持不变,所以我不能集中它,因为它的内容完全填满了整个空间。 div“status”是从另一个文件导入的,它的行为如下所示: 我想把div放在它所在行的垂直中心。有人能帮帮我吗?
我正在处理一个Spring Boot应用程序,其中我使用该应用程序公开SOAP WebService。我在Spring boot应用程序中使用Apache CFX framework for SOAP impl。我正在使用基于注释的方法。 我在一个bean中的Spring Boot配置文件中设置应用程序上下文时遇到了问题。下面是我的代码。 配置文件如下所示。 现在我有了bean SOAPproce
我想上传多个文件到服务器。这是我的角度代码: 控制器的一部分: 问题是在控制器中: “files”列表总是空的(我也尝试了MultipartFile[]而不是list)。只有在指令中返回的不是所有文件,而是一个文件时,它才有效,例如: 代替