0


如何使用此插入上传文件的文件名-> http://www.fengcool.com/2009/06/ajax-form-upload-local-image-file-without-refresh/或 valums.com/ajax -upload/ 在现有的文本输入中?
(我正在使用 php)

文本框前。
<input name="image" type="text" value="file_name" />

4

1 回答 1

0

您必须让实际的上传处理 PHP 页面响应上传文件的文件名。

在 fengcool 的 ajax 中,它在 startUpload() 函数中提供:

var response = $(myFrame.contentWindow.document.body).text();

您可以在需要放置文件名的任何地方使用该“响应”变量。

它实际上作为变量“图像”传递给 addUpload() 函数,您可以对其进行修改以填充您的文本框,大致如下:

document.getElementById("image").value=image

但是,您可能应该<input>以不太通用的方式命名您的名称以避免混淆。

更新,该怎么做:

1)以更独特的方式命名您的文本框,例如:

<input id="uploaded_image_name" type="text" value="" />

// 另请注意,我使用“id”而不是“name”,以便能够使用 Javascript 函数 getElementById()

2) 使用 fengcool 的 ajax,并像这样更改函数 addUpload():

function addUpload(id,img){
   var div = $(document.createElement('div')).attr('id',id);

   //add uploaded image
   div.html("<img src='"+img+"'><br />");
   document.getElementById("uploaded_image_name").value=img

   //add text box
   var fileName = img.substring(img.lastIndexOf("/")+1);
   var txtbx = $(document.createElement('input')).attr('name','img[]').attr('type','text').val(fileName);
   /* you may want to change textbox to a hidden field in production */
   //var txtbx = $(document.createElement('input')).attr('name','img[]').attr('type','hidden').val(fileName);
   txtbx.appendTo(div);


   //add remove thumbnail link
   var rem = $(document.createElement('a'))
                               .attr('alt',id)
                               .attr('href','javascript:;')
                               .text("Remove").click(removeUpload);      
   rem.appendTo(div);

   //add to the page
   div.appendTo("#uploaded_thumb");
}

请注意,唯一的变化是在函数中添加了第四个命令。

于 2011-01-07T15:40:13.550 回答