This is a migrated thread and some comments may be shown as answers.

File Download not working

1 Answer 163 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Jayasankar
Top achievements
Rank 1
Jayasankar asked on 27 Jun 2014, 08:09 AM
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


1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 02 Jul 2014, 06:03 AM
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.

 
Tags
Ajax
Asked by
Jayasankar
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or