0

我有一个数据网格视图,在其中一个单元格中我必须提供下载文件选项。我正在动态创建 xml 文件,当用户单击下载按钮时,他/她应该能够这样做.. . 我迷路了

private void DownloadFile(ScriptInfo scriptInfo)
        {
            XDocument doc =
                         new XDocument(
                           new XElement("scriptfilenames",
                               new XElement("SqlEye",
                                   new XElement("scriptfilename", new XAttribute("fileName", scriptInfo.FileName),
                                       new XElement("warnings",
                                        scriptInfo.ErrorMessages[RuleAction.Error].Select(x => new XElement("warning", new XAttribute("value", x)))),
                                        new XElement("remarks",
                                        scriptInfo.ErrorMessages[RuleAction.warning].Select(x => new XElement("remark", new XAttribute("value", x))))
                                        ))));

            string fileName = scriptInfo.FileName;
            FileInfo fileInfo = new FileInfo(fileName);
            string fileExtension = ".xml";

            byte[] byteData = null;

            //show save as dialog
            using (SaveFileDialog saveFileDialog1 = new SaveFileDialog())
            {
                //Set Save dialog properties
                saveFileDialog1.Filter = "Files (*" + fileExtension + ")|*" + fileExtension;
                saveFileDialog1.Title = "Save File as";
                saveFileDialog1.CheckPathExists = true;
                saveFileDialog1.FileName = fileName;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //what will be here????
                }
            }

        }

需要帮助..

提前致谢

4

3 回答 3

0

如果我正确理解了您的问题,这应该会有所帮助: http: //msdn.microsoft.com/en-us/library/sfezx97z.aspx 检查文件名是否不为空,创建文件流并按照 msdn 上的示例写入数据。

于 2013-05-23T10:22:07.443 回答
0

好的,试试这个,它会完美地工作。如果它是基于服务器的数据库,那么试试这个..

   string s = cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value.ToString();
   File.WriteAllBytes(saveFileDialog1.FileName, byteData);
   byteData = System.Text.Encoding.ASCII.GetBytes(s);

如果您使用的是本地数据库,请尝试以下代码。

    SqlCeConnection cnn = new SqlCeConnection(Properties.Settings.Default.CncConnectionString);
                FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                byte[] SqlTypeBlob = File.ReadAllBytes(fileDialog.FileName);
                cncInfoDataGridView.Rows[dgvCell.RowIndex].Cells[1].Value = fileInfo.Name;
                SqlCeCommand cmd = new SqlCeCommand("INSERT INTO CncInfo (Drawings) VALUES (@DATA)", cnn);
                cmd.Parameters.Add("@DATA", SqlDbType.VarBinary);
                cmd.Parameters["@DATA"].Value = SqlTypeBlob;
                cnn.Open();
                cmd.ExecuteNonQuery();
                cnn.Close();

如果还有什么,那么这个

  byteData = _myAttachments[dgvCell.ColumnIndex];
  File.WriteAllBytes(saveFileDialog1.FileName, byteData);

如果这可行,请关闭它并给我一个批准点击我的答案。平安!!!!!!!

于 2014-07-31T03:34:55.683 回答
0

我知道了

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
                    doc.Save(saveFileDialog1.FileName);                   
}
于 2013-05-23T10:25:51.183 回答