我在我的 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 但它无法正常工作。
您可以将数据从服务器传递到客户端PostedUrl
的财产AjaxFileUploadEventArgs
参数输入UploadComplete
事件处理程序作为 JSON 对象并获取此数据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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)