如何立即显示插入的上传图片? [英] how can i show the Inserted Uploaded Picture instantly?

查看:88
本文介绍了如何立即显示插入的上传图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我编写了以下代码,将上传图像的文件名插入服务器,然后立即显示图像。插入命令正常工作没有问题,同时第二部分代码(立即显示图像)不起作用并将抛出此异常:

IndexOutOfRangeException是cagut。

@ImagePath

我还将Image对象放在了UpdatePanel中。



这也是我的代码:

Hi.
I have written the following code to insert the Uploaded Image's file name into the server and then show the image instantly.the insert command work properly with no problem, meanwhile the second part of the code (displaying image instantly) doesn't work and will throw this exception :
IndexOutOfRangeException was cagut.
@ImagePath
I have also placed Image object inside and UpdatePanel.

and also this is my code:

protected void SendImageButton_Click(object sender, EventArgs e)
       {
           string ImageName = FileUpload.FileName.ToString();
           string ImagePath = Server.MapPath("~/ImageStorage/");
           string ImageFullPath = ImagePath + @"\" + ImageName;
           string ImageUrl = "~/ImageStorage/" + ImageName;
           int ImageSize = FileUpload.PostedFile.ContentLength;

           if (FileUpload.HasFile)
           {
               FileUpload.SaveAs(ImageFullPath);
               SqlCommand Command = DataProvider.GenerateCommand("[dbo].[Insert_UploadPicture_SP]", CommandType.StoredProcedure);
               Command.Parameters.AddWithValue("@IdCode", GlobalVariables.IdCode);
               Command.Parameters.AddWithValue("@ImagePath", ImageUrl);
               Command.Parameters.AddWithValue("@ImageName", ImageName);
               Command.Parameters.AddWithValue("@ImageSize", ImageSize);

               try
               {
                   Command.Connection.Open();
                   Command.ExecuteNonQuery();
                   Command.Connection.Close();
               }
               catch (Exception ex)
               {
                   EventLogger.Log(ex.Message, LogType.Error);
               }
               finally
               {
                   DataProvider.DisposeCommand(Command);
               }
           }

           SqlCommand ReadCommand = DataProvider.GenerateCommand("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
           try
           {
               ReadCommand.Connection.Open();
               SqlDataReader DataReader = DataProvider.ExecuteDataReader("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);
               while (DataReader.Read())
               {
                   PersonelImage.ImageUrl = DataReader["@ImagePath"].ToString();
               }
               ReadCommand.Connection.Close();
           }
           catch (Exception ex)
           {
               EventLogger.Log(ex.Message, LogType.Error);
           }
           finally
           {
               DataProvider.DisposeCommand(ReadCommand);
           }



       }

推荐答案

替换:

Replace:
PersonelImage.ImageUrl = DataReader["@ImagePath"].ToString();



with


with

PersonelImage.ImageUrl = DataReader["YourImagePathColumnName"].ToString();//Remove @ symbol



还有一件事。更改:


One more thing. Change:

SqlDataReader DataReader = DataProvider.ExecuteDataReader("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);



到某事谎言:


to something lie:

SqlDataReader ImageDataReader = DataProvider.ExecuteDataReader("[dbo].[Select_UploadPicture_SP]", CommandType.StoredProcedure);



然后使用如下:


and then use like:

PersonelImage.ImageUrl = ImageDataReader["YourImagePathColumnName"].ToString();


这篇关于如何立即显示插入的上传图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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