1

I'm trying to upload files (images, to be precise) to Cloudinary. In Cloudinary's documentation for .NET (Documentation), it's said that files should be uploaded like this:

var uploadParams = new ImageUploadParams()
{
    File = new FileDescription(@"c:\sample.jpg")
};
var uploadResult = cloudinary.Upload(uploadParams);

So basically, I need file path to upload an image. When I hard-code the path it works, but I want the user be able to choose his own files, obviously. The only way to get the files that I have found was using IFormFile, but the only way I can upload file using it is by uploading it to server first, and then getting the path of the uploaded file and forwarding it to Cloaudinary, but there must be a better way.

So how can I let user upload files to Cloudinary using ASP.NET Core?

2
  • 2
    see File Uploads section in ASP.NET Core documentation Commented Mar 13, 2017 at 14:50
  • Refer to this tutorial techcerberus.blogspot.com.ng/2017/10/… public ImageUploadResult UploadImage(HttpPostedFileBase file) { if (file != null) { var uploadParams = new ImageUploadParams { File = new FileDescription(file.FileName, file.InputStream), Transformation = new Transformation().Width(200).Height(200).Crop("thumb").Gravity("face") }; var uploadResult = _cloudinary.Upload(uploadParams); return uploadResult; } return null; } Commented Oct 6, 2017 at 2:07

2 Answers 2

8

Basically, Cloudinary allows you to upload files from Paths or from Streams.

For File Path:

var uploadParams = new ImageUploadParams()
        {
            File = new FileDescription(@"c:\sample.jpg")
        };
        var uploadResult = cloudinary.Upload(uploadParams);

For Stream:

HttpPostedFile file = fileInput.PostedFile;
var uploadParams = new ImageUploadParams()
{
    File = new FileDescription(file.FileName, file.InputStream),
};
var uploadResult = cloudinary.Upload(uploadParams);

This second method allows you to even post a byte[] image content or a Base64 String containing an image data which you can easily convert to a Stream for upload.

//data is a byte[]
    Stream stream = new MemoryStream(data);
            var cloudinary = Connect();
            var uploadParams = new ImageUploadParams()
            {
                File = new FileDescription(GetGuid.Short(), stream),
                PublicId = GetGuid.Short()
            };
            var uploadResult = cloudinary.Upload(uploadParams);
            return uploadResult.SecureUri.AbsoluteUri;
Sign up to request clarification or add additional context in comments.

2 Comments

I am having a very similar issue here. In my case I am getting my file from the InputFile Blazor component and whenever I try to use the upload method, I get a System.Threading.CancellationToken exception
@Tito, Are you passing cancellation token to the method?
-1

You don't have to upload image to server.

You can't send them directly from the client to cloudinary.

Check their documentation how to achieve this with Jquery.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.