react backend uploadfile

 public List<string> WriteFile(List<UploadDTO> uploads) {
            List<string> fileNames = new List<string>();
            try {
                uploads.ForEach(item => {
                    string FilePath = _serverSettings.SafetyRulePath;
                    DirectoryInfo di = new DirectoryInfo(FilePath);
                    string fileclass = "";
                    string path = FilePath + "\\" + Guid.NewGuid() + item.Extension;
                    byte[] bytes = item.content;

                    if (bytes.Length > 0) {
                        if (bytes.Length / 1024 / 1024 > CustomeMessage.FILESIZE) {
                            throw new SafetyException(CustomeMessage.S_SAFETYRULE_FILESIZERROR);
                        }

                        for (int i = 0; i < 2; i++) {
                            fileclass += bytes[i].ToString();
                        }
                        if (fileclass != "3780") {
                            throw new SafetyException(CustomeMessage.S_SAFETYRULE_FILEERROR);
                        }
                    }

                    if (!di.Exists) {
                        di.Create();
                    }
                    fileNames.Add(path);
                    using (var stream = File.Create(path)) {
                        stream.Write(bytes, 0, bytes.Length);
                    }
                });

            } catch (IOException) {
                throw new SafetyException(CustomeMessage.S_SAFETYRULE_SAVEFILERROR);
            }
            return fileNames;
        }
 public class UploadDTO {
        public string uid { get; set; }
        public string fileName { get; set; }
        public long fileSize { get; set; }
        public byte[] content { get; set; }
        public string Extension { get; set; }
    }

 

posted @ 2022-05-05 14:54  Tammytan  阅读(26)  评论(0编辑  收藏  举报