简体   繁体   中英

How to upload a directory in .Net Core Web Api?

I saw some tutorials about file/files upload in .net core but I can't see tutorials for uploading directory. So, I want to upload all files and folders which are in the selected folder.(the folder itself is also included.)

For example there is a directory tree like this(Directory A contains file1.txt and directory B . Directory B contains only file2.txt ):

A
|   
-file1.txt
-B
 |
 -file2.txt

and I want to upload directory A to server directory D\E\F . So I choose the directory A in GUI. After the uploading, I want to see the directory A content in D\E\F\A path.

In this below code, I can choose a folder in front end, but it only copy one file of the folder. What should I do?

I have a very basic HTML(Angular) like this:

<div class="row" style="margin-bottom:15px;">
  <div class="col-md-3">
    <input webkitdirectory directory type="file" #file placeholder="Choose file" (change)="uploadFile(file.files)" style="display:none;">
    <button type="button" class="btn btn-success" (click)="file.click()">Upload File</button>
  </div>
  <div class="col-md-4">
    <span class="upload" *ngIf="progress > 0">
      {{progress}}%
    </span>
    <span class="upload" *ngIf="message">
      {{message}}
    </span>
  </div>
</div>

TypeScript(Angular) :

      public uploadFile = (files) => {
        if (files.length === 0) {
          return;
        }

        let fileToUpload = <File>files[0];
        const formData = new FormData();
        formData.append('file', fileToUpload, fileToUpload.name);

        this.httpClient.post(`http://192.168.1.10:58432/file`, formData, {reportProgress: true, observe: 'events'})
          .subscribe(event => {


 if (event.type === HttpEventType.UploadProgress)
          this.progress = Math.round(100 * event.loaded / event.total);
        else if (event.type === HttpEventType.Response) {
          this.message = 'Upload success.';
          this.onUploadFinished.emit(event.body);
        }
      });
  }

Server(.net core) side:

[HttpPost("file")]
public async Task UploadFile(IFormFile file)
{
    var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"\\UBUNTU-N55SL\\cloudStorage", file.FileName);
    using (var fileStream = new FileStream(filePath, FileMode.Create))
    {
        await file.CopyToAsync(fileStream);
    }

}

Directories are different from files in that they're not data as such, but a logical structuring of storage. As directories are handled in different ways depending on the operating system of the server, they won't be included in a file upload operation. You could look at using FTP but for the purposes of your code you've posted, you will need to loop through the directories and files you want to upload, and place them in the desired target directories using your code logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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