我在asp.net页面上使用ajaxfileupload控件。上传图像后,我调用uploadcomplete方法将图像保存在磁盘上并使用以下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
);
现在我点击一个保存按钮,尝试使用以下代码获取图像:
Path.GetFileName(EditPhotoImage.ImageUrl) // shows old image
or
Path.GetFileName(PhotoAFU.FileName) // it shows actual image name not encrypted one
但它们都显示旧图像而不是当前图像或实际图像名称未加密名称。如何在此方法中从上面的方法获取文件名?我尝试使用viewstate,但它无法正常工作。
答案 0 :(得分:2)
您可以在PostedUrl
事件处理程序中的AjaxFileUploadEventArgs
参数的UploadComplete
属性中将数据从服务器传递到客户端作为JSON obeject,并在客户端的OnClientUploadComplete
处理程序中获取此数据:
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;
}