上传文件下载后全是System.Byte[]帮我看下上传文件的代码,哪里出错了呢
一个按钮用于选择文件并获得文件信息,
//该按钮用于选择文件并获得文件信息,一个按钮用于插入文件,image为文件字段,插入files.
private void button1_Click(object sender, EventArgs e)
{
try {
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
fileAddress.Text = dialog.FileName;
FileInfo info = new FileInfo(@fileAddress.Text);
//获得文件大小
fileSize = info.Length;
//提取文件名,三步走
int index = info.FullName.LastIndexOf(".");
fileName = info.FullName.Remove(index);
fileName = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
//获得文件扩展名
fileType = info.Extension.Replace(".", "");
//把文件转换成二进制流
files = new byte[Convert.ToInt32(fileSize)];
FileStream file = new FileStream(fileAddress.Text, FileMode.Open, FileAccess.Read);
read = new BinaryReader(file);
read.Read(files, 0, Convert.ToInt32(fileSize));
}
} catch (Exception ex)
{
MessageBox.Show("选择文件时候发生了 " + ex.Message);
}
}
//该按钮用于插入文件,image为文件字段,插入files.
private void button2_Click(object sender, EventArgs e)
{
if (fileAddress.Text.Length <= 0)
{
MessageBox.Show(this, "请选择一个文件", "文件地址不合法", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
return;
}
try
{
string sql = "INSERT INTO imageandfile(image,name,size,type) VALUES('" + files + "','" + fileName + "','" + fileSize + "','" + fileType + "')";
datacon.ExecuteSQL3(sql);
MessageBox.Show("保存成功!!");
}
catch (Exception ex)
{
MessageBox.Show("保存 " + fileName + " 时候发生了 " + ex.Message);
}
}
一个按钮用于选择文件并获得文件信息,
//该按钮用于选择文件并获得文件信息,一个按钮用于插入文件,image为文件字段,插入files.
private void button1_Click(object sender, EventArgs e)
{
try {
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
fileAddress.Text = dialog.FileName;
FileInfo info = new FileInfo(@fileAddress.Text);
//获得文件大小
fileSize = info.Length;
//提取文件名,三步走
int index = info.FullName.LastIndexOf(".");
fileName = info.FullName.Remove(index);
fileName = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
//获得文件扩展名
fileType = info.Extension.Replace(".", "");
//把文件转换成二进制流
files = new byte[Convert.ToInt32(fileSize)];
FileStream file = new FileStream(fileAddress.Text, FileMode.Open, FileAccess.Read);
read = new BinaryReader(file);
read.Read(files, 0, Convert.ToInt32(fileSize));
}
} catch (Exception ex)
{
MessageBox.Show("选择文件时候发生了 " + ex.Message);
}
}
//该按钮用于插入文件,image为文件字段,插入files.
private void button2_Click(object sender, EventArgs e)
{
if (fileAddress.Text.Length <= 0)
{
MessageBox.Show(this, "请选择一个文件", "文件地址不合法", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading);
return;
}
try
{
string sql = "INSERT INTO imageandfile(image,name,size,type) VALUES('" + files + "','" + fileName + "','" + fileSize + "','" + fileType + "')";
datacon.ExecuteSQL3(sql);
MessageBox.Show("保存成功!!");
}
catch (Exception ex)
{
MessageBox.Show("保存 " + fileName + " 时候发生了 " + ex.Message);
}
}
解决方案 »
- Winform中如何自定义卸载程序
- c# 事件调用
- window mobile 下如何获取手机SIM card的ID 和存储卡的ID?
- 怎样像Excel冻结窗口一样冻结DataGrid???
- 在做Form时后台数据库尽量不要用BIT类型,即Form用CHECKBOX控件,Form没法同时查询TRUE和FALSE的数据
- C#如何获取串口号对应的串口(设备)名称?
- 请教:Winform下的DataGrid绑定ComboColumn的问题。
- C#如何解析hl7,我自己写的代码,在读取的时候不太会,菜鸟求指教。
- 寻物启事,能帮我找回的 1000 分侍候。
- 请问server 2003的Version.Major是多少?
- 如何在状态栏显示信息
- 不是很简单的SQL语句求解, 解决即给分,急!!!!!!!
string sql = "INSERT INTO imageandfile(image,name,size,type) VALUES(@files,'" + fileName + "','" + fileSize + "','" + fileType + "')"; SqlParameter _Paramert = new SqlParameter("@files", SqlDbType.Image);
_Paramert.Value = files;
SqlCommand _SqlCommand = new SqlCommand(sql, conn);
_SqlCommand.Parameters.Add(_Paramert);
这行又提示此错误int P_int_returnValue=doperate.Insertfile(@files,fileName ,fileSize.ToString(),fileType);
SqlParameter _Paramert = new SqlParameter("@files", SqlDbType.Image);
_Paramert.Value = files;我调用了一下这个Insertfile方法:
public int Insertfile(Image image2, string name, string size,string type)
{
SqlConnection sqlcon = datacon.getcon3();
SqlCommand sqlcom = new SqlCommand("proc_insertFile", sqlcon);
sqlcom.CommandType = CommandType.StoredProcedure;
sqlcom.Parameters.Add("@image2", SqlDbType.Image).Value = image2;
sqlcom.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = name;
sqlcom.Parameters.Add("@size", SqlDbType.NVarChar, 20).Value = size;
sqlcom.Parameters.Add("@type", SqlDbType.NVarChar, 20).Value = type;
SqlParameter returnValue = sqlcom.Parameters.Add("returnValue", SqlDbType.Int, 4);
returnValue.Direction = ParameterDirection.ReturnValue;
sqlcon.Open();
try
{
sqlcom.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sqlcom.Dispose();
sqlcon.Close();
sqlcon.Dispose();
}
int P_int_returnValue = (int)returnValue.Value;
return P_int_returnValue;
}
#endregion提示:
错误 2 参数“1”: 无法从“byte[]”转换为“System.Drawing.Image” E:\net学习\控制网页\打样短信程序\wgh\Wenjian\Filemanage.cs 96 59 wgh