简体   繁体   English

使用Angular ngf-select上传名称中具有特殊字符的文件

[英]Upload file having special character in name using Angular ngf-select

I am working on Angular2 application with spring boot in back-end. 我正在后端使用Spring Boot的Angular2应用程序上工作。 In application there is feature to upload file on server. 在应用程序中,具有在服务器上上传文件的功能。 The code to upload file working fine. 上传文件的代码工作正常。

I am using ngf-select to choose file. 我正在使用ngf-select选择文件。 It works well when file name is without special characters. 当文件名不包含特殊字符时,它可以很好地工作。

<input type="file" name="file" ng-model="obj.fileModel" ngf-select accept="file_extension">

The problem occurs when I choose file having special character like my name neelam shrma @@ !! 当我选择具有特殊字符(例如我的名字neelam shrma @@! %%% ggg %.json %%% ggg%.json

Upload js code is : 上传js代码为:

var formData = new FormData();
formData.append("fileObj", obj.fileModel);
UploadFile.save(formData, onUploadSuccess, onUploadError);

As file obj.fileModel having File object with special character name, so it do not call resource method to upload file 由于文件obj.fileModel具有带有特殊字符名称的File对象,因此它不调用资源方法来上传文件

Also I have tried by updating the file name withe encoding, but as file object is read-only, so won't allowing me to change the name, also created new file object with encoded file name, but didn't worked. 我也尝试过使用编码来更新文件名,但是由于文件对象是只读的,所以不允许我更改名称,也无法使用编码后的文件名创建新的文件对象,但是没有用。

Resource Method : 资源方法:

public boolean uploadMyFile(MultipartFile mpfile) throws IOException {
...
....
 File file = new File(myFolder + File.separator + file.getOriginalFilename());               
 mpfile.transferTo(file);
......
......
}

So the file having special character not calling my resource method : uploadMyFile(..) and throwing 500 server error. 因此,具有特殊字符的文件未调用我的资源方法:uploadMyFile(..)并抛出500服务器错误。

How to resolve the issue of uploading file having special character using angularJs ngf-select? 如何解决使用angularJs ngf-select上传具有特殊字符的文件的问题?

Add a filter as below 如下添加过滤器

.filter('decodeURL', function () {
    return function (text) {
        if (text) {
            return window.encodeURIComponent(text);
        }
    }
});

and below html 和以下html

<img src="{{itemDetails.LargeImageName | decodeURL}}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM