GridView中的FileUpload问题 [英] Problem with FileUpload in gridview

查看:116
本文介绍了GridView中的FileUpload问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

请帮助我如何从Gridview中的PostedFile中读取流,但是HasFile属性在GridView ASP:FileUpload控件中为true.已通过此文件找到FileUpload

Dear All,

Please, Help how can I read stream from the PostedFile in Gridview However HasFile property is true in GridView ASP:FileUpload control. FileUpload has been found through this

FileUpload FileUpload1 = (FileUpload)row.Cells[0].FindControl("FileUpload1");



并使用此代码段将数据保存到数据库中,但执行 FileUpload1.PostedFile.InputStream.Read(pic,0,len)后, byte [] pic 中的所有零(0)值; 这句话.



and Used this snippet to save the data in Database but byte[] pic has all zero (0) values in it after execute FileUpload1.PostedFile.InputStream.Read(pic, 0, len); this statement.

if(FileUpload1.HasFile)
{
      int len = FileUpload1.PostedFile.ContentLength;
      byte[] pic = new byte[len];
      FileUpload1.PostedFile.InputStream.Read(pic, 0, len);      
}



解决方案是什么?....将数据加载到byte []图片中,但是此代码段在Gridview之外运行良好.



what would be the solution?.... to load data in byte[] pic however this snippet is working fine out side the Gridview.

推荐答案

尝试一下,

Try this,

if (FileUpload1.HasFile)
  {
    string contentType = FileUpload1.PostedFile.ContentType;

    // Get the bytes from the uploaded file
    byte[] fileData = new byte[FileUpload1.PostedFile.InputStream.Length];
    FileUpload1.PostedFile.InputStream.Read(fileData, 0, fileData.Length);
}


我的解决方案.....


我必须通过流媒体来工作...
首先添加..
My Solution.....


I have to work by Streaming...
first of all add..
System.IO



在RowCommand的gridview中

上传文件...



in the gridview on RowCommand

Upload Document...

FileUpload1.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);

// Allocate your data to DB nVarBinary field.
oelScanImages.ImageFile = bytes;

 br.Close();
 fs.Close();



感谢所有参与协助我的人.



Thanks to all who participate to assist me.


您好,
试试这个...

Hi,
Try this...

Byte[] imgByte = null;
try
{
   if (FileUpload1.HasFile && FileUpload1.PostedFile != null)
   {
      //To create a PostedFile
      HttpPostedFile File = FileUpload1.PostedFile;
      //Create byte Array with file len
      imgByte = new Byte[File.ContentLength];
      //force the control to load data in array
      File.InputStream.Read(imgByte, 0, File.ContentLength);
      originalSize = File.ContentLength;
   }
 }
 catch
 {

 }



如果有帮助,请投票作为答案...

问候,

代数



Please vote as answer if help...

Regards,

Algem


这篇关于GridView中的FileUpload问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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