利用webservice接口上传文件…

一、Web Service端
新建->项目->ASP.NET Web服务应用程序
//Service.cs
public class Service : System.Web.Services.WebService
{
      public Service () {

              //如果使用设计的组件,请取消注释以下行
              //InitializeComponent();
      }
      //[WebMethod]
      //public string HelloWorld() {
      //      return "Hello World";
      //}

     
      [WebMethod]
      public int UploadFile(string fileName, int Length, byte[] file_data)
        //上传二进制文件

              string constr = "server=127.0.0.1;database=tb_file;Integrated Security=SSPI;";
              SqlCommand      cmd = new SqlCommand();
              SqlConnection con = new SqlConnection(constr);

              //连接数据库
              cmd.Connection = con;

              //获取或设置一个值,该值指示如何解释 CommandText 属性,CommandType.Text指SQL 文本命令
              cmd.CommandType = CommandType.Text;

              //连接打开
              if (con.State == 0)
                      con.Open();

              cmd.CommandText = "insert into tb_file (filename,filesize,filedata) values( @filename, @filesize, @filedata)";

              SqlParameter spFilename = new SqlParameter("@filename", SqlDbType.VarChar);
              spFilename.Value = fileName;
              cmd.Parameters.Add(spFilename);
              SqlParameter spFile = new SqlParameter("@filedata", SqlDbType.VarBinary);//SqlDbType.VarBinary:Byte 类型的 Array。二进制数据的可变长度流,范围在 1 到 8,000 个字节之间。如果字节数组大于 8,000 个字节,隐式转换会失败。在使用比 8,000 个字节大的字节数组时,请显式设置对象。
              spFile.Value = file_data;
              cmd.Parameters.Add(spFile);
              SqlParameter spFilesz = new SqlParameter("@filesize", SqlDbType.Int);
              spFilesz.Value = Length;
              cmd.Parameters.Add(spFilesz);             

              //执行前面构造的sql查询命令
              int count = cmd.ExecuteNonQuery();

              //服务器收到客户端发送的这个数据以后,先保存在数据库中,,然后顺便在本地保存一个副本
              string str_SavePath = "F:\C#学习作业、\WebClient\";
              FileStream newfs = new FileStream(str_SavePath + fileName, FileMode.Create, FileAccess.Write);
              newfs.Write(file_data, 0, Length);
              newfs.Close();

              con.Close();
              return count;
         
}

二、客户端上传
新建一个应用程序
[转载]利用webservice接口上传文件,并将文件内容保存在数据库中,数据库表


//Form1.cs
namespace WebClient
{
      public partial class Form1 : Form
      {
              //OpenFileDialog表示一个通用对话框,用户可以使用此对话框来指定一个或多个要打开的文件的文件名。
              OpenFileDialog op = new OpenFileDialog();

              public Form1()
              {
                      InitializeComponent();
              }

           
              private void button1_Click(object sender, EventArgs e)
              {
                      //浏览上传文件
                      if (op.ShowDialog() == DialogResult.OK)//DialogResult表示对话框的返回值是 OK(通常从标签为“确定”的按钮发送)。
                      {
                              this.textBox1.Text = Path.GetFullPath(op.FileName);
                      }
              }

                           
              private void button2_Click(object sender, EventArgs e)
              {
                      //使用指定的路径、创建模式和读/写权限初始化 FileStream 类的新实例
                      FileStream    fs = new FileStream(this.textBox1.Text, FileMode.Open, FileAccess.Read);

                      //为指定的流初始化 StreamReader 类的新实例。
                  StreamReader sr  = new StreamReader(fs);

                  byte[] file_data = new byte[(int)fs.Length];
                  fs.Read(file_data, 0, (int)fs.Length);
                  string file_name = Path.GetFileName(op.FileName);

                      //WebClientCon为对WebService端的Web引用
                      WebClientCon.Service obj = new WebClientCon.Service();
                      try
                      {
                              obj.UploadFile(file_name, (int)fs.Length, file_data);
                              MessageBox.Show("上传成功!");
                      }
                      catch (System.Exception ex)
                      {
                            MessageBox.Show(ex.Message.ToString());
                      }
              }

             
              private void button3_Click(object sender, EventArgs e)
              {
                      SqlConnection conn = new SqlConnection();
                      conn.ConnectionString = "Server=127.0.0.1;uid=sa;pwd=123456;database=tb_file";
                      conn.Open();
                      SqlCommand cmd = new SqlCommand("select * from tb_file", conn);
                      SqlDataReader drNew = cmd.ExecuteReader();
                      if (drNew.HasRows)
                      {
                              while (drNew.Read())
                              {
                                      listBox1.Items.Add( drNew[0].ToString() + ":" + drNew[1].ToString());
                              }
                      }
                      drNew.Close();
              }
      }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值