Uploading files via Client Library

Hello,

Could I get some insight on potentially what is happening here? I currently have a controller setup to take in uploaded files, I then pass those to the CreateAssetAsync method. The record is created but then when I view the asset it is empty/nothing is actually stored. It’s just a blank photograph, almost like the content from the stream wasn’t picked up.

Any idea on what could be wrong here?

[HttpPost("Upload")]
    public async Task Upload(List<IFormFile> Files)
    {
        RequestInfo requestInfo = (RequestInfo)HttpContext.Items["RequestInfo"];
        long size = Files.Sum(f => f.Length);

        // full path to file in temp location
        var filePath = Path.GetTempFileName();

        foreach (var formFile in Files)
        {
            if (formFile.Length > 0)
            {

                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await this._assetService.Upload(requestInfo.Tenant, formFile.FileName, formFile.ContentType, stream);
                }
            }
        }
    }

public async Task Upload(Tenant tenant, string contentName, string mimeType, Stream stream)
    {
        this._clientManager = this._squidexClient.GetClient(tenant.Id);
        SquidexAssetClient _assetManager = this._clientManager.GetAssetClient();
        await _assetManager.CreateAssetAsync(contentName, mimeType, stream);
    }

FileMode.Create is wrong.

Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. This requires Write permission. FileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate. If the file already exists but is a hidden file, an UnauthorizedAccessException exception is thrown.

Does it work now? …

Hi Sebastian,

Apologies I have been out of the office for a bit. I tried all different types of Enums. Could you provide a more clear solution? Hate to be a bother, but cannot figure out why it’s not uploading properly.

Thank you very much.

FileMode.Create could overwrite and clear your existing file. But I have just seen that the error is somewhere else:

You create a temportary file:

var filePath = Path.GetTempFileName();

and then you open a stream to this temporary file:

using (var stream = new FileStream(filePath, FileMode.Create))

Which is of course empty.

What you have to do is using the following method:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.iformfile.openreadstream?view=aspnetcore-2.1#Microsoft_AspNetCore_Http_IFormFile_OpenReadStream

[HttpPost("Upload")]
public async Task Upload(List<IFormFile> Files)
{
    foreach (var formFile in Files)
    {
        if (formFile.Length > 0)
        {
            using (var stream = formFile.OpenReadStream()) // !!!!
            {
                await this._assetService.Upload(requestInfo.Tenant, formFile.FileName, formFile.ContentType, stream);
            }
        }
    }
}
1 Like

Worked perfect.

Thank you very much for the explanation.