0

我已经通过二进制文件成功上传并下载了一个txt文件到本地sql,这里有这个代码。

byte[] FileData = File.ReadAllBytes(txtUpload.Text);

            SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
            con.Open();

            SqlCommand cmd = new SqlCommand(@"INSERT into upload (Path,Data,Username,Email,Price,Description)values(@path,@data,@username,@email,@price,@description)", con);
            cmd.Parameters.Add(new SqlParameter("@path", (object)txtUpload.Text));
            cmd.Parameters.Add(new SqlParameter("@data", (object)FileData));
            cmd.Parameters.Add(new SqlParameter("@username", (object)txtSellUsername.Text));
            cmd.Parameters.Add(new SqlParameter("@email", (object)txtSellEmail.Text));
            cmd.Parameters.Add(new SqlParameter("@price", (object)txtSellPrice.Text));
            cmd.Parameters.Add(new SqlParameter("@description", (object)txtSellDescription.Text));


            cmd.ExecuteNonQuery();
            con.Close();

并像这样下载

FileStream FS = null;
        byte[] dbbyte;

        SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM upload where Id='" + txtBuyId.Text + "'", con);
        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        con.Close();

        if (dt.Rows.Count > 0)
        {
            dbbyte = (byte[])dt.Rows[0]["Data"];

            string filepath = (rootDirectory + "\\Profiles\\" + txtBuyPrice.Text.ToString() + ".txt" );
            FS = new FileStream(filepath, System.IO.FileMode.Create);
            FS.Write(dbbyte, 0, dbbyte.Length);
            FS.Close();
            con.Close();
        }*/

从这里我已经弄清楚如何通过这样的网络服务上传

  try
        {
            byte[] FileData = File.ReadAllBytes(txtUpload.Text);
            UserDetailsService.SellProfiles sellprofiles = new UserDetailsService.SellProfiles();

            sellprofiles.Upload = txtUpload.Text;
            sellprofiles.Upload2 = FileData;
            sellprofiles.Username = txtSellUsername.Text;
            sellprofiles.Email = txtSellEmail.Text;
            sellprofiles.Price = txtSellPrice.Text;
            sellprofiles.Name = txtSellProfileName.Text;
            sellprofiles.Description = txtSellDescription.Text;
            obj.InsertSellingProfiles(sellprofiles);

            DialogResult dialogResult = MessageBox.Show("Your profile has been successfully uploaded!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (dialogResult == DialogResult.OK)
            {
                new Menu().Show();
                this.Close();
            }

但是现在我尝试了多种方法从 Web 服务下载二进制数据,但我正在努力寻找解决方案!

谁能帮我这个?希望我的代码可以帮助更有经验的人帮助我找到解决方案

谢谢

我试图从这样的服务下载:

  UserDetailsService.Service1Client obj1 = new UserDetailsService.Service1Client();
        byte[] dbbyte;
        FileStream FS;

        UserDetailsService.Service1Client sc = new UserDetailsService.Service1Client();
        if (sc.CheckIfProfileExists(txtBuyId.Text, txtBuyProduct.Text))
        {
            BuyProfile.Id = txtBuyId.Text;
            BuyProfile.Name = txtBuyProduct.Text;

            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt = obj1.DownLoadProfile(txtBuyId.Text, txtBuyProduct.Text);
            if (dt.Rows.Count > 0)
            {
                dbbyte = (byte[])dt.Rows[0]["Data"];

                string filepath = (rootDirectory + "\\Profiles\\" + txtBuyProduct.Text.ToString() + ".txt");
                FS = new FileStream(filepath, System.IO.FileMode.Create);
                FS.Write(dbbyte, 0, dbbyte.Length);
                FS.Close();
4

1 回答 1

0

您可以像这样在您的网络服务中编写一个方法

public byte[] GetFileBytes(strubg someParam)
{
   //read from database and return here
}
于 2013-08-29T08:35:34.983 回答