首页 新闻 赞助 找找看

将整个excel文件存入MSSQL数据库中的image字段后,下载后不能打开

0
悬赏园豆:100 [已解决问题] 解决于 2012-07-12 16:34

#region 打开电机各类文档
        /// <summary>
        /// 打开电机各类文档
        /// </summary>
        /// <param name="fileName">文档的名称</param>
        /// <param name="procName">存储过程名</param>
        /// <param name="fieldFileName">存储文档名称的字段名</param>
        /// <param name="fieldFile">存储文档的字段名</param>
        private void openFile(string fileName, string procName,string fieldFileName,string fieldFile)
        {
            dataSet = SqlHelper.ExecuteDataset(SqlHelper.connStr, CommandType.StoredProcedure, procName, new SqlParameter(fieldFileName, fileName));
            table = dataSet.Tables[0];
            string path = @"C:\Users\Administrator\Desktop\ " + fileName;
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                foreach (DataRow row in table.Rows)
                {
                    byte[] fileByte = (byte[])row[fieldFile];
                    int arraySize = fileByte.GetUpperBound(0);
                    fs.Write(fileByte, 0, arraySize);
                    Process.Start(path);
                }   
            } 
        }
        #endregion

这是我的下载代码

using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                        {
                            byte[] motorSPLByte = new byte[Convert.ToInt32(fileStream.Length)];
                            fileStream.Read(motorSPLByte, 0, (int)fileStream.Length);
                            SqlHelper.ExecuteNonQuery(sqlTran, CommandType.StoredProcedure, "proc_insertSPLDecument", new SqlParameter("motorName", this.txtMotorName.Text),
                                new SqlParameter("@sPLName", fileName), new SqlParameter("@sPLDecument", motorSPLByte));
                        }


这是我的部分上传代码

问题补充:

急救啊,这个问题困扰了我很长时间。。。

图片、word都可以打开。。。

紫砂清壶的主页 紫砂清壶 | 初学一级 | 园豆:4
提问于:2012-07-12 14:16
< >
分享
最佳答案
0

代码中的主要问题是在FileStream还没关闭,就用Process.Start打开文件,我改了一下代码:

int index = 1;
foreach (DataRow row in table.Rows)
{
    string path = @"C:\Users\Administrator\Desktop\ " + fileName + index++;
    using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
    {           
            byte[] fileByte = (byte[])row[fieldFile];
            int arraySize = fileByte.Length(0);
            fs.Write(fileByte, 0, arraySize);
        }
    } 
    Process.Start(path);
}
收获园豆:100
dudu | 高人七级 |园豆:31075 | 2012-07-12 14:59

using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
    {          
           
byte[] fileByte = (byte[])row[fieldFile];
           
int arraySize = fileByte.Length(0);
            fs.Write(fileByte,
0, arraySize);
        }
    }
关闭了啊,用using自动销毁FileStream

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:03

@紫砂清壶: Process.Start(path);应该放在using外面

dudu | 园豆:31075 (高人七级) | 2012-07-12 15:06

@dudu: 

foreach (DataRow row in table.Rows)
                {
                    using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        byte[] fileByte = (byte[])row[fieldFile];
                        int arraySize = fileByte.GetUpperBound(0);
                        fs.Write(fileByte, 0, arraySize);
                    }
                    Process.Start(path);
                }

还是同样的问题

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:10

@紫砂清壶: 

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:14

@紫砂清壶: 

你现在每次foreach都是同样的文件名,这个地方会有问题。

dudu | 园豆:31075 (高人七级) | 2012-07-12 15:14

@dudu: 但是为什么word,图片没有问题,就excel有问题???

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:20

@dudu: 难道可能是excel里面含有公式、图片,不能恢复???

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:23

@紫砂清壶: 你上传一个空的Excel文件试试

dudu | 园豆:31075 (高人七级) | 2012-07-12 15:25

@dudu: 可以打开,但是出现警告

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:29

@紫砂清壶: 是用Process.Start(path);打开的,还是手动打开的?

dudu | 园豆:31075 (高人七级) | 2012-07-12 15:31

@dudu: 手动打开和Process.Start(path)打开效果一样。。。能否远程协助一下,我的QQ发到你信箱了???

紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 15:33

@紫砂清壶: 是fileByte.GetUpperBound(0);引起的,fileByte.GetUpperBound(0)得到的长度比fileByte.Length少1,也就是 Array.GetUpperBound(0) = Array.Length-1;

dudu | 园豆:31075 (高人七级) | 2012-07-12 16:31
其他回答(1)
0

呵呵,不用说一看就知道楼主的代码不够简练.

数组长度你调试跟踪一下

迅捷网络[来送福利] | 园豆:616 (小虾三级) | 2012-07-12 14:43

不是数组长度的问题啊

支持(0) 反对(0) 紫砂清壶 | 园豆:4 (初学一级) | 2012-07-12 14:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册