且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在将文件上传到数据库后获取最新上传的文件

更新时间:2023-01-15 12:08:55

你根本不在这里订购。你应该存储它们被添加的日期,然后对它进行排序,或者对id进行排序。那么你可以找到最后一个。


你好,



请不要在每个函数中写连接字符串。



请创建一个全局类或任何其他方式,因此如果您更改连接,则无需在每个函数中更改它。 :)





现在解决方案:



你可以Javascript '或jquery'的异步请求。



如果你想减少服务器负载然后使用缓存,我想说使用一个全局类或表,无论何时你上传文件,更改该类或表的上次上传日期,然后同步或异步地在页面上显示。



异步 [ ^ ]

Hi All,
I''m uploading a File into sqlserver using fileupload in file upload Method i m calling a method which gives latest file uploads, but it is not showning the file last file uploaded
after uploading the file then i need to refresh the page then only it is showing the latest file which i uploaded. without refreshing the page how to get the latest file that uploaded in DB From FIleUpload() method. i m writing the code as below :-

IN Page Load():-
----------------

protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["preview"] == "1" && !string.IsNullOrEmpty(Request.QueryString["fileId"]))
        {
            var fileId = Request.QueryString["fileId"];
            var fileContentType = (string)Session["fileContentType_" + fileId];
            var fileName = (string)Session["fileName_" + fileId];


            string ct = (string)Session["fileContentType_" + fileId];
            using (SqlConnection _con = new SqlConnection("data source=test;Initial Catalog=test;User Id=sa;Password=sql;"))
            using (SqlCommand _cmd = new SqlCommand("UploadFile", _con))
            {
                _cmd.CommandType = CommandType.StoredProcedure;
                _cmd.Parameters.AddWithValue("@FileName", fileName);
                _cmd.Parameters.AddWithValue("@FileType", fileContentType);
                _cmd.Parameters.AddWithValue("@FileContent", (byte[])Session["fileContents_" + fileId]);

                _con.Open();
                _cmd.ExecuteNonQuery();
                _con.Close();
            }

            Response.Clear();
            Response.ContentType = fileContentType;
          
        }

        if (!IsPostBack)
        {
            DataTable fileList = GetFileList();
            gvFiles.DataSource = fileList;
            gvFiles.DataBind();
        }

    }


--------------------------------------
AND GetFile List Method is :-

public static DataTable GetFileList()
    {
        DataTable fileList = new DataTable();
        using (SqlConnection _con = new SqlConnection("data source=test;Initial Catalog=test;User Id=sa;Password=sql;"))
        {
            _con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = _con;
            cmd.CommandTimeout = 0;

            cmd.CommandText = "SELECT ID, FileName FROM Files";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter adapter = new SqlDataAdapter();

            adapter.SelectCommand = cmd;
            adapter.Fill(fileList);

            _con.Close();
        }

        return fileList;
    }

You do no ordering here at all. You should store the date they were added, and sort on that, or sort on the id, I guess. then you can find the last one in.


Hi,

Please don''t write Connection string in every function.

Please create one global class or any other way so if you change the connection then you dont need to change it in every function. :)


Now you solution :

you can you Javascript''s or jquery''s asynchronous request.

If you want less sever load then use caching, I want to say use one global class or table where whenever you upload a file the change the last upload date at that class or table and then show it on your page synchronously or asynchronously.

asynchronously[^]