Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the upload component of ng-zorro to cooperate with msf4j to realize file upload

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use the upload component of ng-zorro and msf4j to realize file upload". In the daily operation, I believe that many people have doubts about how to use the upload component of ng-zorro and msf4j to realize file upload. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "how to use the upload component of ng-zorro to cooperate with msf4j to achieve file upload"! Next, please follow the editor to study!

1. Preparatory work

Ng-zorro 's upload component can upload files, and of course there are other components that can do the same, so I won't discuss anything else here.

Msf4j is a micro-service framework of java, which is used here for back-end upload.

Angular: > = 7.2.0

Node > = 10.15.3

Ng-zorro > = 8.0.1

Java > = 1.8

Msf4j = 2.1.0

two。 Front-end content upload

According to the official documentation of ng-zorro, nzAction is used to configure the upload address. After it is configured, it can be uploaded automatically. You do not have to write your own upload method. Of course, it can also be configured to upload manually. The writing method here is the same as the official document. For more information on other parameters, please see the official document. The way it is written in ts is also consistent with the official.

3.msf4j@Path ("/ file") public class RecordApi {private static Logger logger = LoggerFactory.getLogger (RecordApi.class); / / set the file download address private static String MOUNT_PATH = "/ Users/moon/Downloads/test/"; public RecordApi () {if (OSUtil.isWindowsOS ()) {MOUNT_PATH = "d:\\" }} / * * File upload * @ param files file collection, which uses the nz-upload component and is uploaded in multipart/form-data format The corresponding files is the value of nzName in the component * if the file already exists, the original file * @ return * @ throws SVSException will be overwritten * / @ Path ("/ uploadFile") @ POST public Response uploadFile (@ FormDataParam ("files") List files) throws Exception {files.forEach (f-> {try {) File file = new File (MOUNT_PATH F.getName () If (file.exists ()) {file.delete ();} Files.copy (f.toPath (), Paths.get (MOUNT_PATH, f.getName ());} catch (IOException e) {logger.error ("Error while Copying the file" + e.getMessage (), e) Throw new RuntimeException ("file upload exception");}}); return Response.ok () .build () } / * File download * @ param fileName filename with a suffix, such as: gyy.jpg * @ return * / @ Path ("/ downloadFile/ {fileName}") @ GET public Response downloadFile (@ PathParam ("fileName") String fileName) {File file = Paths.get (MOUNT_PATH, fileName). ToFile () If (file.exists ()) {return Response.ok (file) .build ();} return Response.ok () .build () } / * * delete file * @ param fileName filename with suffix, for example: gyy.jpg * @ return * / @ Path ("/ deleteFile/ {fileName}") @ DELETE public Response deleteFile (@ PathParam ("fileName") String fileName) {File file = new File (MOUNT_PATH, fileName) If (! file.exists ()) {return Response.ok () .build ();} return Response.ok (file.delete ()) .build ();}

After writing, start the micro service of msf4j to upload and download files. The front end cannot directly access the background and needs to go to the proxy. Angular itself can support the configuration of proxies. The configuration of proxies can be referred to this article.

4. Other

It is important to note that:

1. Nz-upload uses the format of multipart/form-data to transmit data, which is in the form of key-value pairs, that is, key-value, so you need to configure a key value in the interface of msf4j, and this key value is the value of nzName in the nz-upload component, so that the file information can be obtained through key in the backend code.

2. The files received in msf4j are in array format, which looks something like this:

{"files": [file1, file2, file3. ]}

So you can copy the file to another location by traversing the file object, for example, it can be transferred to hdfs, and so on.

At this point, the study on "how to use the upload components of ng-zorro to cooperate with msf4j to achieve file upload" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report