【问题标题】:getting asynfileupload controls file name on button click在按钮单击时获取 asyncfileupload 控制文件名
【发布时间】:2013-09-11 13:51:04
【问题描述】:

我在我的 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,但它无法正常工作。

【问题讨论】:

    标签: asp.net ajax ajaxcontroltoolkit


    【解决方案1】:

    您可以在UploadComplete 事件处理程序的AjaxFileUploadEventArgs 参数的PostedUrl 属性中将数据作为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;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-11
      • 2017-11-19
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      • 2011-05-05
      • 1970-01-01
      相关资源
      最近更新 更多