需要帮助将文件拆分为数据库 [英] Need Help With Splitting File Into Database

查看:66
本文介绍了需要帮助将文件拆分为数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友,



我正在使用文件上传控制和文件上传时间文件分成3部分到数据库。文件的每个部分都存储到数据库中的各个列。我的项目,但我没有得到完整的文件路径所以任何人都帮我解决这个问题。



Hi Friends ,

I am using file upload control and file upload time file split into 3 part to database . Each part of the file store to individual columns in database. my project but I failed to get full path of file so anybody help me to solve this problem.

<div>
    <br />
    <asp:Label ID="Label3" Font-Bold="True" runat="server" Text="--File Upload Page--">
    <br />
<br />
</div>
<div">
    <table>
    <tr>
    <td>
        <asp:Label ID="Label1" runat="server" Text="File Name">
    </td>
    <td colspan="1" style="width: 207px">
        <asp:TextBox ID="txtfname" runat="server">
    </td>
    </tr>
    <tr>
    <td>
        <asp:Label ID="Label2" runat="server" Text="File Category">
    </td>
    <td colspan="1" style="width: 207px">
        <asp:TextBox ID="txtfcate" runat="server" TextMode="SingleLine">
    </td>
    </tr>
    <tr>
    <td>
        <asp:Label ID="Label4" runat="server" Text="Group Name">
    </td>
    <td colspan="1" style="width: 207px">
        <asp:DropDownList ID="ddlgname" runat="server" DataSourceID="SqlDataSource1" 

            DataTextField="Groupname" DataValueField="Groupname">
        
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

            ConnectionString="<%$ ConnectionStrings:CloudSystemConnectionString2 %>" 

            SelectCommand="SELECT [Groupname] FROM [Creategroup]">
    </td>
    </tr>
    <tr>
    <td>
        <asp:Label ID="Label5" runat="server" Text="File">
    </td>
    <td colspan="1" style="width: 207px">
        <asp:FileUpload ID="FileUpload1" runat="server" />
    </td>
    </tr>
  <tr>
  <td></td>
  </tr>
    <tr>
    <td></td>
    <td>
        <asp:Button ID="butlogin" runat="server" Text="Upload" Width="44px" 

            onclick="butlogin_Click" />
        <asp:Button ID="butclear" runat="server" Text="Clear" 

            style="margin-left: 25px" onclick="butclear_Click" />
   </td>
   <td></td>
   </tr></table> 





C#代码:



C# code:

protected void butlogin_Click(object sender, EventArgs e)
    {
        
        string files = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("FilesPath\\" + FileUpload1.FileName));
        String path = Path.GetFullPath(Server.MapPath("~\\FilsPath\\" + FileUpload1.FileName));
        ////string input = Path.GetFileName(path);
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "insert into Fileupload values('"  + txtfname.Text + "','" + txtfcate.Text + "','" +
        ddlgname.Text + "','" + @files + "')";
        cmd.Parameters.AddWithValue("@files",files);
        cmd.ExecuteNonQuery();
        con.Close();
        splitfiles(files, 3);
        Response.Write("<script>Alert.Show('Successfully Upload');</script>");
        txtfname.Text = "";
        txtfcate.Text = "";
       
    }
    public bool splitfiles(string SourceFile, int nNoofFiles)
    {
        {
            bool Split = false;
            {

                FileStream fs = new FileStream(SourceFile, FileMode.Open, FileAccess.Read);
                int SizeofEachFile = (int)Math.Ceiling((double)fs.Length / nNoofFiles);
                for (int i = 0; i < nNoofFiles; i++)
                {
                    string baseFileName = Path.GetFileNameWithoutExtension(SourceFile);
                    string Extension = Path.GetExtension(SourceFile);
                    FileStream outputFile = new FileStream(Path.GetDirectoryName(SourceFile) + "\\Split Files\\" + "\\" + baseFileName + "." +
                        i.ToString().PadLeft(5, Convert.ToChar("0")) + Extension + ".tmp", FileMode.Create, FileAccess.Write);


                    int bytesRead = 0;
                    byte[] buffer = new byte[SizeofEachFile];
                    if ((bytesRead = fs.Read(buffer, 0, SizeofEachFile)) > 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                        //outp.Write(buffer, 0, BytesRead);
                        string packet = baseFileName + "." + i.ToString().PadLeft(3, Convert.ToChar("0")) + Extension.ToString();
                    }
                    outputFile.Close();
                }
                fs.Close();
            }
            return Split;
        }
    }

推荐答案

ConnectionStrings:CloudSystemConnectionString2%>

< span class =code-attribute> SelectCommand = SELECT [Groupname] FROM [Creategroup] >
< / td >
< / tr >
< tr >
< td >
< asp:标签 ID = Label5 runat = server 文字 = 文件 >
< / td >
< td colspan = 1 style = width:207px >
< asp:FileUpload ID = FileUpload1 runat = server / >
< / td >
< / tr >
< tr >
< td > < / td >
< / tr >
< tr >
< td > < / td >
< td >
< asp:按钮 ID = butlogin runat = server 文字 = 上传 宽度 = 44px

onclick = butlogin_Click / >
< asp:按钮
ID = butclear runat = server 文字 = 清除

style=\"margin-left: 25px\" onclick=\"butclear_Click\" />
</td>
<td></td>
</tr>< /table>
ConnectionStrings:CloudSystemConnectionString2 %>" SelectCommand="SELECT [Groupname] FROM [Creategroup]"> </td> </tr> <tr> <td> <asp:Label ID="Label5" runat="server" Text="File"> </td> <td colspan="1" style="width: 207px"> <asp:FileUpload ID="FileUpload1" runat="server" /> </td> </tr> <tr> <td></td> </tr> <tr> <td></td> <td> <asp:Button ID="butlogin" runat="server" Text="Upload" Width="44px" onclick="butlogin_Click" /> <asp:Button ID="butclear" runat="server" Text="Clear" style="margin-left: 25px" onclick="butclear_Click" /> </td> <td></td> </tr></table>





C# code:



C# code:

protected void butlogin_Click(object sender, EventArgs e)
    {
        
        string files = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("FilesPath\\" + FileUpload1.FileName));
        String path = Path.GetFullPath(Server.MapPath("~\\FilsPath\\" + FileUpload1.FileName));
        ////string input = Path.GetFileName(path);
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "insert into Fileupload values('"  + txtfname.Text + "','" + txtfcate.Text + "','" +
        ddlgname.Text + "','" + @files + "')";
        cmd.Parameters.AddWithValue("@files",files);
        cmd.ExecuteNonQuery();
        con.Close();
        splitfiles(files, 3);
        Response.Write("<script>Alert.Show('Successfully Upload');</script>");
        txtfname.Text = "";
        txtfcate.Text = "";
       
    }
    public bool splitfiles(string SourceFile, int nNoofFiles)
    {
        {
            bool Split = false;
            {

                FileStream fs = new FileStream(SourceFile, FileMode.Open, FileAccess.Read);
                int SizeofEachFile = (int)Math.Ceiling((double)fs.Length / nNoofFiles);
                for (int i = 0; i < nNoofFiles; i++)
                {
                    string baseFileName = Path.GetFileNameWithoutExtension(SourceFile);
                    string Extension = Path.GetExtension(SourceFile);
                    FileStream outputFile = new FileStream(Path.GetDirectoryName(SourceFile) + "\\Split Files\\" + "\\" + baseFileName + "." +
                        i.ToString().PadLeft(5, Convert.ToChar("0")) + Extension + ".tmp", FileMode.Create, FileAccess.Write);


                    int bytesRead = 0;
                    byte[] buffer = new byte[SizeofEachFile];
                    if ((bytesRead = fs.Read(buffer, 0, SizeofEachFile)) > 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                        //outp.Write(buffer, 0, BytesRead);
                        string packet = baseFileName + "." + i.ToString().PadLeft(3, Convert.ToChar("0")) + Extension.ToString();
                    }
                    outputFile.Close();
                }
                fs.Close();
            }
            return Split;
        }
    }


First off, as Richard told you, never concatenate strings to form an SQL query - i’s very dangerous. Look up \"SQL Injection\" for details.



Secondly, you forgot an \"e\":

First off, as Richard told you, never concatenate strings to form an SQL query - i's very dangerous. Look up "SQL Injection" for details.

Secondly, you forgot an "e":
FileUpload1.SaveAs(Server.MapPath("FilesPath\\" + FileUpload1.FileName));
String path = Path.GetFullPath(Server.MapPath("~\\FilsPath\\" + FileUpload1.FileName));
                                                     ^
                                                     |



Which means the file won’t be found...


Which means the file won't be found...


这篇关于需要帮助将文件拆分为数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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