2

How to upload all files in folder to Google Storage Cloud without loop. Here I try use C# with one file upload :

public async Task<ActionResult> Upload(IFormFile file)
{
    string contentRootPath = _hostingEnvironment.ContentRootPath;

    var credential = GoogleCredential.FromJson(System.IO.File.ReadAllText(contentRootPath + "/credentials.json"));

    StorageClient storageClient = StorageClient.Create(credential);

    var objectName = MakeObjectName();

    var imageObject = await storageClient.UploadObjectAsync(
        bucket: CloudConfig.BUCKET,
        objectName: objectName,
        contentType: file.ContentType,
        source: file.OpenReadStream(),
        options: new UploadObjectOptions { PredefinedAcl = PredefinedObjectAcl.PublicRead }
    );

    return Ok(new DropzoneInfo { Name = file.FileName, ObjectName = objectName, Link = imageObject.MediaLink, Size = file.Length});
}
NguyenHung
  • 77
  • 7

1 Answers1

1

You have a controller action that accepts a single uploaded file. Use a List<IFormFile> in your controller action - see this answer: Submitting multiple files to ASP.NET controller accepting an ICollection<IFormFile>

There is an attribute for uploading directories: <input type="file" webkitdirectory> (How do I use Google Chrome 11's Upload Folder feature in my own code?)

These browsers support it: Edge, Safari, Firefox, Chrome, Opera (https://caniuse.com/#feat=input-file-directory)

Markus Dresch
  • 5,290
  • 3
  • 20
  • 40
  • I just want to question about Google Cloud Storage, not html, c# and other related upload files from front end to back end. – NguyenHung Dec 04 '18 at 07:36
  • @NguyenHung If you want to know if there is a way to upload multiple files in GCS, as you can see in [this example](https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/master/storage/api/Storage/Storage.cs ), there is none. What you would need to do is to use some kind of loop. – mgoya Dec 04 '18 at 12:32