如何在将文件上传到数据库后获取最新上传的文件 [英] how to get latest uploaded File after uploading a file into database

查看:177
本文介绍了如何在将文件上传到数据库后获取最新上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在文件上传中使用fileupload将文件上传到sqlserver方法我调用一个方法来提供最新的文件上传,但是没有显示文件上传的文件<上传文件后,我需要刷新页面,然后只显示我上传的最新文件。无需刷新页面如何获取上传到DB中的最新文件来自FIleUpload()方法。即时编写如下代码: -



IN Page Load(): -

----------- -----

 受保护  void  Page_Load( object  sender,EventArgs e)
{
if (请求。 QueryString [ preview] == 1&&!string.IsNullOrEmpty(Request.QueryString [ fileId]))
{
var fileId = Request.QueryString [ fileId];
var fileContentType =( string )会话[ fileContentType _ + fileId];
var fileName =( string )会话[ fileName _ + fileId];


string ct =( string )会话[ fileContentType _ + fileId];
使用(SqlConnection _con = new SqlConnection( data source = test; Initial Catalog = test; User Id = sa; Password = sql;))
using (SqlCommand _cmd = new SqlCommand( UploadFile,_ c))
{
_cmd.CommandType = CommandType.StoredProcedure;
_cmd.Parameters.AddWithValue( @ FileName,fileName);
_cmd.Parameters.AddWithValue( @ FileType,fileContentType);
_cmd.Parameters.AddWithValue( @ FileContent,( byte [])会话[ fileContents _ + fileId]);

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

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

}

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

}



--------------------- -----------------

和GetFile列表方法是: -



  public   static  DataTable GetFileList()
{
DataTable fileList = new DataTable();
使用(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;
}

解决方案

你根本不在这里订购。你应该存储它们被添加的日期,然后对它进行排序,或者对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[^]


这篇关于如何在将文件上传到数据库后获取最新上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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