图像上传预览

时间:2010-06-29 06:03:00

标签: asp.net

在浏览之后和使用asp.net和vb.net或java脚本在IE 7.0中上传之前预览图像。我已尝试过以下编码

function DoPreview()
{
  var filename = document.form1.filesent.value;
  var Img = new Image();
//    if (navigator.appName == "Netscape")
//    {
//      alert("Previews do not work in Netscape.");
//    }
//    else
//{
  Img.src = filename;
  document.images[0].src = Img.src;
// }
}

function CheckUpload()
{
  var filename = document.form1.filesent.value;
  var extension;
  var valid = true
  var Img1 = new Image()
  if (navigator.appName == "Netscape")
  {
    alert("This upload function cannot be used with Netscape.");
    valid = true;
  }
  else if (filename == '')
  {
    valid = false;
    alert("Please include a file.");
  }
  else
  {  
    extension = filename.substring(filename.length - 3, filename.length);
    if (extension.toUpperCase() != 'jpg')
    {
      valid = false  ;
      alert("The file must be a jpg.");
    }
    else
    {
      Img1.src = filename;
      if ((Img1.height == 0) || (Img1.width == 0))
      {
        valid = false;
        alert("The file is invalid.");
      }
      else
      {
        document.form1.height.value = Img1.height;
        document.form1.width.value = Img1.width;
      }
    }
  }
  return valid
}

<form method="post"  action ="Default.aspx"  enctype="multipart/form-data" name="form1" onsubmit="return CheckUpload()">
    <input type="hidden" name="height" value="0" />
    <input type="hidden" name="width" value="0" />
    <input type="file" name="filesent"  onchange ="DoPreview()" />

    <br/>
 </form>
 <img  id ="imagepreview" src="" name="image1" alt ="Image"  />

它在IIS 5.1中工作但图像预览未在IE 7.0中显示

1 个答案:

答案 0 :(得分:0)

您无法预览在“文件上传”控件中选择的图像。实际上,图像只能通过URL在浏览器中通过服务器的虚拟目录查看。上面的代码显示您正在尝试设置客户端(未存储在服务器中)的图像的预览,这是不可能的。您可以通过将图像存储在服务器端(将图像上传到服务器)来进行图像预览。