越来越asynfileupload控件文件名上单击按钮 [英] getting asynfileupload controls file name on button click

查看:104
本文介绍了越来越asynfileupload控件文件名上单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用我的asp.net页面ajaxfileupload控制。图片上传后,我打电话uploadcomplete方法,使用下面的JavaScript保存图像在磁盘上,并显示在图像控制:

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
                );

现在我点击保存按钮,并尝试使用下面的code获取图像:

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.

推荐答案

您可以通过从服务器到客户端的数据 PostedUrl AjaxFileUploadEventArgs <物业/ code>参数 UploadComplete 事件处理程序JSON obeject并获得在客户端 OnClientUploadComplete 处理这个数据

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;
 }

这篇关于越来越asynfileupload控件文件名上单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆