上传按钮单击后,fileupload控件变为null [英] fileupload control getting null after upload button click

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

问题描述

大家好,



今天我正在使用fileupload,



我在哪里上传按钮(选择img后查看)



n



保存按钮(用img保存数据txtfileds )



但是,点击上传按钮查看img后,文件上传量达到空。

n

我在fileupload中得到null。



任何人都可以建议我如何解决这个问题... plzzzz .....



我的btnupload代码:



  protected   void  btnupload_Click( object  sender,EventArgs e)
{
if (VehiclePhotoUpload.HasFile)
{
Session [ ImageBytes] = VehiclePhotoUpload.FileBytes;
string str = Server.MapPath(VehiclePhotoUpload.FileName);
ImagePreview.ImageUrl = 〜/ ImageHandler.ashx;
}
}





btnsave代码:

 protected void btnUpload_Click(object sender,EventArgs e)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [ConnSQL]。ConnectionString);
try
{
FileUpload img =(FileUpload)imgUpload;
Byte [] imgByte = null;
if(img.HasFile&& img.PostedFile!= null)
{
//创建一个PostedFile
HttpPostedFile File = imgUpload.PostedFile;
//用文件len
创建字节数组imgByte = new Byte [File.ContentLength];
//强制控件加载数组
File.InputStream.Read(imgByte,0,File.ContentLength);
}
//将员工姓名和图像插入db


conn.Open();
string sql =sp_insertingimg;
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue(@ imgname,txtEName.Text.Trim());
cmd.Parameters.AddWithValue(@ imgdata,imgByte);
cmd.ExecuteScalar();
//lblResult.Text = String.Format(员工ID为{0},id);
}
catch
{
//lblResult.Text =出错;
}
最后
{
conn.Close();

}

}





Plzzz ...帮助我,谢谢。

解决方案

可能是您在更新面板中使用FileUpload Control。如果您在保存图像时没有使用此会话,为什么要使用会话来存储图像。


尝试在会话中存储文件路径,或者在您选择的文件中使用其他变量(如字符串)上装载机。显示图像后使用存储文件路径。


在上传按钮...我在会话中存储文件上传

这样

< pre lang =sql> 
session [& quot; fup& quot;] = filupload1;



n

i将其转换为btnsave

as

 fileupload1 =(fileupload)session [& quot; fup& quot;];< / pre> 


Hi guys,

Today i'm working with fileupload,

where i have upload button(to view after selecting an img)

n

save button (to save data txtfileds with img)

But, after clicking upload button to view img, the fileupload is getting to Null.
n
I'm getting null in fileupload.

Can anybody suggest me how can i sort out this... plzzzz.....

my btnupload code:

protected void btnupload_Click(object sender, EventArgs e)
        {
            if (VehiclePhotoUpload.HasFile)
            {
                Session["ImageBytes"] = VehiclePhotoUpload.FileBytes;
                string str = Server.MapPath(VehiclePhotoUpload.FileName);
                ImagePreview.ImageUrl = "~/ImageHandler.ashx";
            }
        }



btnsave code:

protected void btnUpload_Click(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnSQL"].ConnectionString);
           try
           {
               FileUpload img = (FileUpload)imgUpload;
               Byte[] imgByte = null;
               if (img.HasFile && img.PostedFile != null)
               {
                   //To create a PostedFile
                   HttpPostedFile File = imgUpload.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);
               }
               // Insert the employee name and image into db


               conn.Open();
               string sql = "sp_insertingimg";
               SqlCommand cmd = new SqlCommand(sql, conn);
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.Parameters.AddWithValue("@imgname", txtEName.Text.Trim());
               cmd.Parameters.AddWithValue("@imgdata", imgByte);
               cmd.ExecuteScalar();
               //lblResult.Text = String.Format("Employee ID is {0}", id);
           }
           catch
           {
               //lblResult.Text = "There was an error";
           }
           finally
           {
               conn.Close();

           }

       }



Plzzz... help me, Thanks.

解决方案

may be you are using FileUpload Control in a Update Panel. why do you use session to store image if you are not using this session in saving image.


try to store file path in session or in other variable like string for file which u have selected in up-loader. After showing image use stored file path.


in upload button... i stored fileupload in session
like this

<pre lang="sql">
 session[&quot;fup&quot;] = filupload1;


n
i converted it in btnsave
as

fileupload1 = (fileupload)session[&quot;fup&quot;];</pre>


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

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