Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i rename the file before saving it to server i want to based it on the username ? example: mypic.jpg
this is my code:
ASP.NET
<pre>  public IActionResult OnPostMyUploader(IFormFile MyUploader)
        {
            // No point loading and resizing the image if we're not going to use it:
            if (MyUploader is null) return new ObjectResult(new { status = "fail" });

            using var image = System.Drawing.Image.FromStream(MyUploader.OpenReadStream());
            using var resized = new Bitmap(image, new System.Drawing.Size(150, 150));
            using var imageStream = new MemoryStream();
            resized.Save(imageStream, ImageFormat.Jpeg);

       
            string uploadsFolder = @"\\10.10.10.67\AQSImages\IdPictures\";
            string filePath = Path.Combine(uploadsFolder, MyUploader.FileName);
            using var fileStream = new FileStream(filePath, FileMode.Create);

            // Reset the current position of the stream:
            imageStream.Seek(0L, SeekOrigin.Begin);
            imageStream.CopyTo(fileStream);

            return new ObjectResult(new { status = "success" });
            //using var image = Image.Load(MyUploader.OpenReadStream());
            //image.Mutate(x => x.Resize(256, 256));
            ////var image = Image.FromStream(MyUploader.OpenReadStream());
            ////var resized = new Bitmap(image, new Size(256, 256));
            ////using var imageStream = new MemoryStream();
            ////resized.Save(imageStream, ImageFormat.Jpeg);
            ////var imageBytes = imageStream.ToArray();

            //if (MyUploader != null)
            //{
            //    string uploadsFolder = @"\\10.10.10.67\AQSImages\IdPictures\";
            //    string filePath = Path.Combine(uploadsFolder, MyUploader.FileName);
            //    using (var fileStream = new FileStream(filePath, FileMode.Create))
            //    {
            //     image.CopyTo(fileStream);
            //    }
            //    return new ObjectResult(new { status = "success" });
            //}
            //return new ObjectResult(new { status = "fail" });

            //////Better use extension method
            ////string customRoot = @"\\10.10.10.67\AQSImages\IdPictures\";
            ////if (!Directory.Exists(customRoot))
            ////{
            ////    Directory.CreateDirectory(customRoot);
            //}
        }


This is the username input type in my view:
HTML
<pre><div class="col-4">
                                    <label class="w-100">Username</label>
                                    <input id="empUsername" class="input-form w-100" disabled>
                                </div>


What I have tried:

javascript - Upload and change a new name for image in ASP.NET MVC - Stack Overflow[^]
Posted
Updated 23-Jun-22 18:25pm

1 solution

Just change this bit:
C#
string uploadsFolder = @"\\10.10.10.67\AQSImages\IdPictures\";
string MyUserDetailsIWantToAdd = $"...";
string filePath = Path.Combine(uploadsFolder, MyUSerDetailsIWantToAdd + MyUploader.FileName);
using var fileStream = new FileStream(filePath, FileMode.Create);
Just fill in the bit "..." with the user details.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900