Reputation: 27996
I am using ajaxfileupload control on my asp.net page. After image is uploaded, I call uploadcomplete method to save the image on disk and show in image control using following javascript:
string fileName = Guid.NewGuid() + Path.GetExtension(PhotoAFU.FileName.Trim()); // encrypt filename
string filePath = Path.Combine(storagePath, fileName);
string fileVirtPath = GetImageUrl(fileName);
int rnd = new Random((int)DateTime.Now.Ticks).Next(1, 1000);
ScriptManager.RegisterClientScriptBlock(PhotoAFU, PhotoAFU.GetType(), "img",
String.Format(
@"top.document.getElementById('{0}').src='{1}?x={2}';
top.document.getElementById('{3}').value = '{4}'",
EditPhotoImage.ClientID,
fileVirtPath,
rnd,
UploadedImageFileNameHF.ClientID,
fileName),
true
);
Now I click on a save button and try to get the image using following code:
Path.GetFileName(EditPhotoImage.ImageUrl) // shows old image
or
Path.GetFileName(PhotoAFU.FileName) // it shows actual image name not encrypted one
but they both show old image not the current image or actual image name not encrypted name. How can I get the filename from above method in this method ? I tried using viewstate but it is not working properly.
Upvotes: 2
Views: 773
Reputation: 22448
You can pass data from server to client in PostedUrl
property of AjaxFileUploadEventArgs
parameter in UploadComplete
event handler as JSON obeject and get this data in OnClientUploadComplete
handler on client side:
protected void AjaxFileUpload1_OnUploadComplete(object sender, AjaxFileUploadEventArgs e)
{
string fileName = Guid.NewGuid().ToString();
string fileVirtPath = "foobar";
e.PostedUrl = string.Format("{{ fileName: '{0}', imageSrc: '{1}?x={2}' }}",
fileName, fileVirtPath, new Random((int)DateTime.Now.Ticks).Next(1, 1000));
}
function AjaxFileUpload1_OnClientUploadComplete(sender, args) {
var fileInfo = Sys.Serialization.JavaScriptSerializer.deserialize(args.get_postedUrl());
$get("<%= EditPhotoImage.ClientID %>").src = fileInfo.imageSrc;
$get("<%= UploadedImageFileNameHF.ClientID %>").value = fileInfo.fileName;
}
Upvotes: 2