Downloading files from grid

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Hello community

I'm having problems in downloading files from a grid, i saw some solutions, all very similar, and but none solve my problem, i guess there must be some small mistake from my own. Can you help me?
I'm using layers architecture and telerik framework
ASCX User control:
<telerik:GridTemplateColumn DataField="FileContent" HeaderText="Download" SortExpression="FileContent" UniqueName="FileContent" FilterControlAltText="Filter FileContent column">
                                    <ItemTemplate>
                                        <asp:LinkButton runat="server"
                                             ID="lnkDownload"  Text="Download" CommandName="download_file" CommandArgument='<%# Eval("Ficheiro") %>' OnClick="link1_Click">
                                        </asp:LinkButton>
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>

ACSX.CS:
protected void gvTimesheets_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "download_file")
            {
                GridDataItem item = (GridDataItem)e.Item;

                var itemIndex = e.CommandArgument;

            }

protected void link1_Click(object sender, EventArgs e)
        {
            var model = listagembll.GetFileByID(ID);
            if (model.FileContent != null)
            {
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Type", "Application/octet-stream");
                Response.AddHeader("Content-Length", model.FileContent.Length.ToString());
                Response.AddHeader("Content-Disposition", "inline; ficheiro=" + model.Ficheiro + model.FileTipo); // inline ou attachment
                Response.BinaryWrite(model.FileContent);
                Response.Flush();
                Response.End();
            }
        }

BLL method to get the row in the grid:
public ListagemTimesheet GetFileByID(int ID)
        {
            using (GestaoProjectosEntities lt = new GestaoProjectosEntities())
            {
                return lt.ListagemTimesheets.FirstOrDefault(a => a.ID == ID);
            }
        }
.
 
Moving to 3rd party...
 
Did you set breakpoints on those methods you showed us? All of them being hit?
 
Hello. I was able to successfully made the download with some small fixes in the code above and by adding the following OnClientClick method, solving a conflict between Binary.Write method and Ajax implementation
Function for disabling ajax during download execution:
  function onRequestStart(sender, args) {

                           try {

                               if (args.get_eventTarget().indexOf("lnkDownload") >= 0) {

                                   args.set_enableAjax(false);

                                   return true;

                               }

                           }

                           catch (ex) {

                               alert("Error pushing postback: " + ex.message);

                           }

                       }
Unfortunatly i have some big new problems now
The download is made, but the file content is only the name of the file, a file named "test", when oppened, shows as the only content the word "test".
In addition, the page continues to load infinitily after download is concluded.

Edit: Just had an flash idea, i don´t have an insert function yet, so i introduced the regists manually in db, by the following method " 'teste', filetype: 'txt', CAST('teste.txt' AS VARBINARY(MAX)" being "teste and txt" the file name and format, and the only content that appeares in the file "teste.txt". Is this method incorrect, and the cause of my problem?
 
Last edited:
Are you doing WebForms or MVC?
 
Looks to be a case of GIGO.

CAST('teste.txt' AS VARBINARY(MAX) will cast that string to a byte array. It won't magically reach out to the file system and read the file contents as a byte array.
 
SQL is not my experty, i found this the easyeast way to INSERT... i was generally reviewing my code and in fact seems senseless..
And about the loading problem, do you have any idea?
 
Sorry, I'm anti-SQL so take what I write next with a huge grain of salt.
1) SQL databases in general are not mean to store large blobs of binary data. Yes, you can do it, but most DBAs will give you the evil eye as they grudgingly create such a table or database. The correct thing to do is a text field that has a path that points to the copy of the file on the filesystem.
2) You need to read the file data yourself, and then pass in those bytes to the your SqlCommand that does the insert as a parameter.
 
Sorry, I'm anti-SQL so take what I write next with a huge grain of salt.
1) SQL databases in general are not mean to store large blobs of binary data. Yes, you can do it, but most DBAs will give you the evil eye as they grudgingly create such a table or database. The correct thing to do is a text field that has a path that points to the copy of the file on the filesystem.
2) You need to read the file data yourself, and then pass in those bytes to the your SqlCommand that does the insert as a parameter.
thank you for the advices!
 

Latest posts

Back
Top Bottom