File download creation problem RRS feed

  • Question

  • Hi,

    I am having trouble creating a file download from an InfoPath form. The form is in a SharePoint list, and has C# code in the background.

    When I click a button on the form, it enters the function CreateReport() where I create a file and want the user to download that file.

    The code looks like this:

    // create file on savePath (file creation works fine) 
    FileStream file = new FileStream(savePath, FileMode.Open); 
    long fileSize = file.Length; 
    byte[] data = new byte[(int)fileSize]; 
    file.Read(data, 0, (int)file.Length); 
    HttpContext.Current.Response.ContentType = "application/ms-excel"; 
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Report.xls");

    The problem is, when I press the button, the file is created (that part is okay), but I get an error message saying:

    Critical error

    An error occurred submitting this form to the Web server. The form will now be closed.

    Click Start Over to load a new copy of the form. If this error persists, contact the support team for the Web site.

    Click Close to exit this message.

    I tried using Response.Write and Response.WriteFile instead of BinaryWrite, and HttpContext.Current.ApplicationInstance.CompleteRequest() instead of Response.End(), but the result was the same.

    I tried debugging it too, but no exception is thrown, so I'm really lost here and I don't know what the problem could be. 

    Thursday, May 26, 2011 9:38 AM


  • The problem here is that you are trying to return the file in the HTTP response, but Forms Server is also trying to render the form in the HTTP response as well. There are two main options I can think of that you could do.

    One is to redirect the user, either directly to the URL of the file, or to another non-infopath-form page which contains your http response code to return the file (this could be a custom layouts/application .ASPX page in SharePoint for example).

    The other option is to create your report file in a location, such as a SharePoint library, then display a link to it in your InfoPath form, the user can then click on the link and will be prompted to op/save etc.



    • Proposed as answer by D-Virdi Friday, May 27, 2011 2:23 PM
    • Marked as answer by Leoyi Sun Wednesday, June 1, 2011 9:02 AM
    Thursday, May 26, 2011 1:15 PM