使用 C#/.NET 将图像上传到服务器并将文件名存储在数据库中 [英] Upload image to server using C#/.NET and storing filename in DB

查看:37
本文介绍了使用 C#/.NET 将图像上传到服务器并将文件名存储在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码段将数据插入到我的数据库中的表中.它工作得很好.但是,我想开始添加文件名数据,但不知道如何继续.

I'm currently using the following snippet to insert data into a table in my database. It works great. But, I want to start adding filename data and not sure how to proceed.

我有以下几点:

// Create command 
comm = new SqlCommand(
  "INSERT INTO Entries (Title, Description) " +
  "VALUES (@Title, @Description)", conn);

// Add command parameters
comm.Parameters.Add("@Description", System.Data.SqlDbType.Text);
comm.Parameters["@Description"].Value = descriptionTextBox.Text;
comm.Parameters.Add("@Title", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["@Title"].Value = titleTextBox.Text;

我还有一个文件上传选项.但是,我不知道如何使用它来执行以下操作:

I also have a File Upload option. But, I don't know how to use this to do the following:

  • 将文件移动到我的 images 目录和
  • filename 值存储在我的表中.
  • move the file to my images directory and
  • store the filename value in my table.

我已将正确的 enctype 添加到我的表单中,但现在有点丢失.

I have added the correct enctype to my form but now a little lost.

有人能解释一下最好的方法吗?

Can someone explain the best way to do this?

非常感谢您对此提供的任何帮助.

Many thanks for any help with this.

推荐答案

要将文件存储在 images 文件夹中,应该是:

To store the file in an images folder, it should be:

FileUpload1.SaveAs(Server.MapPath("~/Images/" + FileUpload1.FileName));

然后在文件名中添加命令参数

and then add the command parameters in the fileName

comm.Parameters["@FileName"].Value = FileUpload1.FileName;

注意:您的数据库表中必须有 FileName 字段.

Note: you must have the FileName field in your DB table.

这篇关于使用 C#/.NET 将图像上传到服务器并将文件名存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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