File Download not working

2 posts, 0 answers
  1. Jayasankar
    Jayasankar avatar
    1 posts
    Member since:
    Jun 2014

    Posted 27 Jun 2014 Link to this post

    In a grid am having an image button ; on click o to open a file that is there in database.

    My code is like this

    protected void imgBtnPersonalView_Click(object sender, ImageClickEventArgs e)
    {
            
                ImageButton imgBtnPersonalView = (ImageButton)sender;
    GridDataItem dataItem = (GridDataItem)imgBtnPersonalView.NamingContainer;

    string documentId = dataItem["Document_Id"].Text;
    DataTable dt = objNewJoineeFormBL.GetDocumentUploadByDocumentId(documentId);
    byte[] objData = (byte[]) dt.Rows[0]["Data"];
    string strFileName = dt.Rows[0]["Document_Name"].ToString();
    string fileExtension = dt.Rows[0]["File_Format"].ToString();

    Response.Clear();
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = fileExtension;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName);
    Response.BinaryWrite(objData);
    Response.Flush();
    Response.End();
    }

    I can see the data while debugging..
    But when it comes to the last line, it shows an error like
    0xc00ce514 - JavaScript runtime error: Could not complete the operation due to error c00ce514
    i am attaching screen shot of it.
    What i need is, a file window to open/ save button.
    i got files in picture format, doc and pdf.


    Thanks in advance


  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 02 Jul 2014 Link to this post

    Hi Jayasankar,

    It seems that the application uses AJAX. If that is the case you should force the imgBtnPersonalView button to perform a postback instead of an AJAX request. For that purpose you can cancel the request by subscribing to the InitializeRequest event.

    JavaScript:
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(initRequest);
                function initRequest(sender, args) {
                    if (args.get_postBackElement().id.indexOf("imgBtnPersonalView") != -1) {
                        args.set_cancel(true);  //stop async request
                        sender._form["__EVENTTARGET"].value = args.get_postBackElement().id.replace(/\_/g, "$");
                        sender._form["__EVENTARGUMENT"].value = "";
                        sender._form.submit();
                        return;
                    }
                }

    More information about this approach can be found here.

    Regards,
    Angel Petrov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top