0

I am using the code below to display an uploaded picture in asp.net . The Image name is change to a unique name and args.get_fileName retrieves the original name of the file instead of the new unique name.

How can I retrieve the new unique name:

protected void FileUploadComplete(object sender, EventArgs e)
{
    var date = DateTime.Now.ToString("yyyy-MM-dd-hh_mm_ss");
    string filename = System.IO.Path.GetFileName(AsyncFileUpload1.FileName);
    AsyncFileUpload1.SaveAs("~/listingImages/" + date + filename);
}

Script:

function uploadStarted() {
    $get("imgDisplay").style.display = "none";
}

function uploadComplete(sender, args) {
    var imgDisplay = $get("imgDisplay");
    imgDisplay.src = "images/loader.gif";
    imgDisplay.style.cssText = "";
    var img = new Image();
    img.onload = function () {
        imgDisplay.style.cssText = "height:100px;width:100px";
        imgDisplay.src = img.src;
    };

    img.src = "/listingImages/" + args.get_fileName();

    $('#bodyContent_Label1').html("/listingImages/" + args.get_fileName());
}
2
  • Could you just pass the filename into the view via a viewmodel? Commented Aug 10, 2015 at 9:09
  • @Canvas thank you for reply. I am new to asp.net and not sure of how i will do this. Thanks Commented Aug 10, 2015 at 10:14

1 Answer 1

1

You should use password generating concept

private static string CreateRandomPassword(int passwordLength)
{
 string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
 char[] chars = new char[passwordLength];
 Random rd = new Random();

 for (int i = 0; i < passwordLength; i++)
 {
  chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
 }

 return new string(chars);
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, i am really new to asp.net and have no idea how to inpelment this concept with my exciting code
you know about AJAX ??
You can call CreateRandomPassword() function using AJAX and you will easily get return value as unique string
What should be lenth of my randomly generated string?
Fist of all that fellow want unique string, so we can use CreateRandomPassword Method. Than Random function will give integer number randomly so we can use allowedChars[rd.Next(0, allowedChars.Length)]; for creating unique string.
|

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.