File name when uploading RRS feed

  • Question

  • User-1209166642 posted

    When uploading an image, and if the image is named rover.jpg, and I want the uploaded image to be rover456.jpg, how do I name it?

    This Github example uses a random name:

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.Extensions.Configuration;
    using SampleApp.Utilities;
    namespace SampleApp.Pages
        public class BufferedMultipleFileUploadPhysicalModel : PageModel
            private readonly long _fileSizeLimit;
            private readonly string[] _permittedExtensions = { ".txt" };
            private readonly string _targetFilePath;
            public BufferedMultipleFileUploadPhysicalModel(IConfiguration config)
                _fileSizeLimit = config.GetValue<long>("FileSizeLimit");
                // To save physical files to a path provided by configuration:
                _targetFilePath = config.GetValue<string>("StoredFilesPath");
                // To save physical files to the temporary files folder, use:
                //_targetFilePath = Path.GetTempPath();
            public BufferedMultipleFileUploadPhysical FileUpload { get; set; }
            public string Result { get; private set; }
            public void OnGet()
            public async Task<IActionResult> OnPostUploadAsync()
                if (!ModelState.IsValid)
                    Result = "Please correct the form.";
                    return Page();
                foreach (var formFile in FileUpload.FormFiles)
                    var formFileContent = 
                        await FileHelpers
                                formFile, ModelState, _permittedExtensions, 
                    if (!ModelState.IsValid)
                        Result = "Please correct the form.";
                        return Page();
                    // For the file name of the uploaded file stored
                    // server-side, use Path.GetRandomFileName to generate a safe
                    // random file name.
                    var trustedFileNameForFileStorage = Path.GetRandomFileName();
                    var filePath = Path.Combine(
                        _targetFilePath, trustedFileNameForFileStorage);
                    // **WARNING!**
                    // In the following example, the file is saved without
                    // scanning the file's contents. In most production
                    // scenarios, an anti-virus/anti-malware scanner API
                    // is used on the file before making the file available
                    // for download or for use by other systems. 
                    // For more information, see the topic that accompanies 
                    // this sample.
                    using (var fileStream = System.IO.File.Create(filePath))
                        await fileStream.WriteAsync(formFileContent);
                        // To work directly with the FormFiles, use the following
                        // instead:
                        //await formFile.CopyToAsync(fileStream);
                return RedirectToPage("./Index");
        public class BufferedMultipleFileUploadPhysical
            public List<IFormFile> FormFiles { get; set; }
            [StringLength(50, MinimumLength = 0)]
            public string Note { get; set; }

    There has got to be an easier way to do a simple upload of images, and name them as wanted.

    Saturday, April 25, 2020 6:57 PM

All replies

  • User1034446946 posted

    you don't have to rename the file at all, the example just demonstrates one possible issue and one option on how to solve it.

    If you do upload an file with the same name it could overrite, more than likely without you knowing and that could cause massive issues. the folder may also not have the necessary permissions which would casue the side to fail.

    You could write code to lookup the folder and check the folder for a file with the same name and if there is,reject the upload an ask the user to change the name or allow them to type another name (which will also need to be validated).

    Its up to you.

    Sunday, April 26, 2020 1:44 AM
  • User711641945 posted

    Hi jimap_1:

    When uploading an image, and if the image is named rover.jpg, and I want the uploaded image to be rover456.jpg, how do I name it?

    Did you want to rename the image?

    Just change the following code which based on this thread:

    var path = Path.Combine(_env.ContentRootPath, "wwwroot/images/upload", "rover456.jpg");



    Best Regards,


    Monday, April 27, 2020 9:26 AM