说做过文件上传与下载的程序。。asp.net的

解决方案 »

  1.   

    fileuploat 控件拉一个就成了
      

  2.   


    create database FilesDB
    go
    use FilesDB
    go
    create table Files
    (
    Id Int IDENTITY primary key,
    FileName NVarchar(50),
    UpFilePath Nvarchar(255),
    UpTime DateTime default getdate()
    )
    aspx        <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
                DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="download" CommandArgument='<%#Eval("upFilePath") %>'>下载</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowDeleteButton="True" />
                    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
                        SortExpression="Id" />
                    <asp:BoundField DataField="FileName" HeaderText="FileName" SortExpression="FileName" />
                    <asp:BoundField DataField="UpFilePath" HeaderText="UpFilePath" SortExpression="UpFilePath" />
                    <asp:BoundField DataField="UpTime" HeaderText="UpTime" SortExpression="UpTime" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLExpress;Initial Catalog=FilesDB;Integrated Security=True"
                DeleteCommand="DELETE FROM [Files] WHERE [Id] = @Id" InsertCommand="INSERT INTO [Files] ([FileName], [UpFilePath], [UpTime]) VALUES (@FileName, @UpFilePath, @UpTime)"
                ProviderName="System.Data.SqlClient" SelectCommand="SELECT [Id], [FileName], [UpFilePath], [UpTime] FROM [Files]"
                UpdateCommand="UPDATE [Files] SET [FileName] = @FileName, [UpFilePath] = @UpFilePath, [UpTime] = @UpTime WHERE [Id] = @Id">
                <DeleteParameters>
                    <asp:Parameter Name="Id" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="FileName" Type="String" />
                    <asp:Parameter Name="UpFilePath" Type="String" />
                    <asp:Parameter Name="UpTime" Type="DateTime" />
                    <asp:Parameter Name="Id" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="FileName" Type="String" />
                    <asp:Parameter Name="UpFilePath" Type="String" />
                    <asp:Parameter Name="UpTime" Type="DateTime" />
                </InsertParameters>
            </asp:SqlDataSource>
    aspx.cs        protected void Button1_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile)
                {
                    FileUpload1.SaveAs(MapPath(FileUpload1.FileName));
                    const string cnString = @"Server=.\SQLExpress;Integrated Security=True;database=FilesDB";
                    SqlConnection cn = new SqlConnection(cnString);
                    SqlCommand cmd = new SqlCommand("insert into Files(FileName,upFilePath,UpTime) values(@fileName, @upFilePath, getdate())", cn);
                    cmd.Parameters.AddWithValue("@fileName", FileUpload1.FileName);
                    cmd.Parameters.AddWithValue("@upFilePath", MapPath(FileUpload1.FileName));
                    cn.Open();
                    cmd.ExecuteNonQuery();
                    cn.Close();                DataBind();
                }
            }        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if (e.CommandName == "download")
                {
                    FileStream fileStream = new FileStream(e.CommandArgument.ToString(), FileMode.Open);
                    long fileSize = fileStream.Length;
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(e.CommandArgument.ToString(), System.Text.Encoding.UTF8) + "\"");
                    Response.AddHeader("Content-Length", fileSize.ToString());
                    byte[] fileBuffer = new byte[fileSize];
                    fileStream.Read(fileBuffer, 0, (int)fileSize);
                    fileStream.Close();
                    Response.BinaryWrite(fileBuffer);
                    Response.End(); 
                }
                DataBind();
            }
      

  3.   

    靠。楼上正解。。结贴给分,我来段简单的
    string filepath = "", fileExtName = "", mFileName, mPath;
            System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息:<hr color=red>");
            string s = filepath.Substring(filepath.LastIndexOf(".") + 1).ToLower();
            filepath = FileUp.PostedFile.FileName; //获取目录
            fileExtName = filepath.Substring(filepath.LastIndexOf(".") + 1);//获取扩展名 
            if (filepath.ToString()!= "" && fileExtName.ToLower() == "rar")
            {                       
                //substring获取扩展名
                try
                {
                    mPath = Server.MapPath("upfile/");
                    mFileName =filepath.Substring(filepath.LastIndexOf("\\") + 1);               
                    FileUp.PostedFile.SaveAs(mPath + mFileName);
                }
                catch (Exception ex)
                { Response.Write(ex.Message); }
            }
            else 
            {
               Response.Write(("<script language='javascript'>alert('发生错误,上传失败,可能是上传的文件没有压缩');</script>"));
            }分吧。我也来一段。我来最简单的。
      

  4.   

    拜托,这不就是下载的代码么
                    FileStream fileStream = new FileStream(e.CommandArgument.ToString(), FileMode.Open);
                    long fileSize = fileStream.Length;
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(e.CommandArgument.ToString(), System.Text.Encoding.UTF8) + "\"");
                    Response.AddHeader("Content-Length", fileSize.ToString());
                    byte[] fileBuffer = new byte[fileSize];
                    fileStream.Read(fileBuffer, 0, (int)fileSize);
                    fileStream.Close();
                    Response.BinaryWrite(fileBuffer);
                    Response.End();