[CLOSED] Download file in MVC have error

Page 1 of 2 12 LastLast
  1. #1

    [CLOSED] Download file in MVC have error

    Hi:

    I have a issue about downloading file in MVC.
    In my page, I have a form named "fileform" the markup is:
    <form id="fileform" class="x-hide-display">
    </form>
    I also have a button with a client click handler:
    <ext:Button ID="BtnExportExcel" runat="server" Text="<%$Resources:Strings,ExportExcel%>">
                    <Listeners>
                        <Click Fn="exportExcel"></Click>
                    </Listeners>
    </ext:Button>
    On Javascript file
    the method "exportExcel" is like this:
    var exportExcel= function () {
            Ext.net.DirectMethod.request({
                    url: $shell.resolveUrl("~/Planning/CorePlanning/ExportExcel"),
                    isUpload: true,
                    formProxyArg: "downloadForm",
                    buffer: 300,
                    cleanRequest: true,
                    params: {
                        titles: "TestValue",
                        options: "TestValue"
                    },
                    success: function(result) {
                         //Success process.....//
                    },
                    failure: function(msg, response) {
                         //Failure process.....//
                    }
                });
        }
    In the controller, the method which is called is:
    public AjaxResult ExportExcel(string titles, string options)
            {
                AjaxResult result = new AjaxResult();
                result.IsUpload = true;
                using (var memoryStream = new MemoryStream())
                {
                    //....................
                   //Create the content for memoryStream
                    
                   Response.AddHeader("Content-Disposition",
                                               string.Format("attachment; filename=test.xsl"));
                    result.Result=File(memoryStream.ToArray(), "application/vnd.ms-excel");
                }
                return result;
            }
    Now when I click the button I got a internal error which is code 500.
    The error message is about like
    A potentially dangerous Request.Form value was detected from the client (titles=&quot;...tem &amp;nbsp;&lt;button type=\&quot;butto...&quot;).
    Could you help sort out this problem please?
    Thank you
    Last edited by Daniil; Aug 02, 2011 at 1:01 PM. Reason: [CLOSED]
  2. #2
    Hi,

    I think you just set up the wrong form's id here:
    formProxyArg: "downloadForm",
    According to this:
     <form id="fileform" class="x-hide-display">
    
    </form>
    it must be
    formProxyArg: "fileform",
    Also I should say that "success"/"failure" handlers are not fired when a file is downloaded.
  3. #3
    Quote Originally Posted by Daniil View Post
    Hi,

    I think you just set up the wrong form's id here:
    formProxyArg: "downloadForm",
    According to this:
     <form id="fileform" class="x-hide-display">
    
    </form>
    it must be
    formProxyArg: "fileform",
    Also I should say that "success"/"failure" handlers are not fired when a file is downloaded.
    Aha, thank you Daniil.
  4. #4
    Quote Originally Posted by Daniil View Post
    Hi,

    I think you just set up the wrong form's id here:
    formProxyArg: "downloadForm",
    According to this:
     <form id="fileform" class="x-hide-display">
    
    </form>
    it must be
    formProxyArg: "fileform",
    Also I should say that "success"/"failure" handlers are not fired when a file is downloaded.
    Daniil, I changed the formProxyArg with the correct form id. Now the error message has changed.

    it said "BADRESPONSE: Unexpected token <"

    any ideas about that? Thank you
  5. #5
    Ok, here is my working example. Please provide your one to reproduce the problem.

    Example View

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head runat="server">
        <title>Ext.Net.MVC Example</title>
    
        <script type="text/javascript">
            var download = function () {
                Ext.net.DirectMethod.request({
                    url           : "/Test/Download/",
                    isUpload      : true,
                    formProxyArg  : "fileform",
                    cleanRequest  : true,
                    params        : {
                        url : "~/Default.aspx" 
                    }
                });
            };
        </script>
    
    </head>
    <body>
        <ext:ResourceManager runat="server" />
        
        <form id="fileform" class="x-hide-display">
        </form>
        <ext:Button runat="server" Text="Download">
            <Listeners>
                <Click Handler="download();" />
            </Listeners>
        </ext:Button>
    </body>
    </html>
    Example Controller Action

    public ActionResult Download(string url)
    {
        var path = Server.MapPath(url);
        var file = new FileInfo(path);
        if (file.Exists)
        {
            return File(url, "application/octetstream", "test.aspx");
        }
    
        AjaxResult r = new AjaxResult(string.Format("alert('{0} is not exists')", path));
        r.IsUpload = true;
        return r;
    }
  6. #6
    Em... I use your code and modified. First error is about the parameters sending.
    I can not pass the array or object as parameters.
    My modified code is below:

    Example View
    <%@ Page Language="C#" %>
     
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head runat="server">
        <title>Ext.Net.MVC Example</title>
     
        <script type="text/javascript">
            var download = function () {
                Ext.net.DirectMethod.request({
                    url           : "/Test/Download/",
                    isUpload      : true,
                    formProxyArg  : "fileform",
                    cleanRequest  : true,
                    params        : {
                        paraStrings: getParametersStringArray(),
                        paraObject: getParametersObject()
                    }
                });
            };
            
            var getParametersObject=function(){
               var result=new Object();
               result.Id="1";
               result.Name="Test name";
               return result;
            };
    
            var getParametersStringArray=function(){
               var result=[];
               result.push("Test_1.xls");
               result.push("Test_2.xls");
               return result;
            };
    
        </script>
     
    </head>
    <body>
        <ext:ResourceManager runat="server" />
         
        <form id="fileform" class="x-hide-display">
        </form>
        <ext:Button runat="server" Text="Download">
            <Listeners>
                <Click Handler="download();" />
            </Listeners>
        </ext:Button>
    </body>
    </html>
    Example Controller Action
    public ActionResult Download(string[] paraStrings, TestObjext paraObject)
    {
        var path = Server.MapPath(paraStrings[2]);
        var file = new FileInfo(path);
        if (file.Exists && paraObject!=null)
        {
            return File(url, "application/octetstream", "test.xls");
        }
       
        AjaxResult r = new AjaxResult(string.Format("alert('{0} is not exists')", path));
        r.IsUpload = true;
        return r;
    }
    
    public class TestObjext 
    {
        public int Id {get; set;}
        public string Name {get; set;}
    }
  7. #7
    You should encode the parameters:
    paraStrings : Ext.encode(getParametersStringArray()),
    paraObject : Ext.encode(getParametersObject())
    Last edited by Daniil; Aug 02, 2011 at 10:11 AM.
  8. #8
    Quote Originally Posted by Daniil View Post
    You should encode the parameters:
    paraStrings : Ext.encode(getParametersStringArray()),
    paraObject : Ext.encode(getParametersObject())
    Hi Daniil, I add the Ext.encode but still does not work.
    in the controller method, the paraStrings has one string and it's like this:
    ["Test_1.xls","Test_2.xls"]
    and the paraObject is still null as before.
  9. #9
    I forgot that parameters of a controller action are not deserialized automatically.

    Please deserialize manually.

    And you don't need to call Ext.encode() manually, it is done automatically.

    Example

    params  : {
        jsonParaStrings: getParametersStringArray(),
        jsonParaObject: getParametersObject()
    }
    
    public ActionResult Download(string jsonParaStrings, string jsonParaObject)
    {
        string[] paraStringsD = JSON.Deserialize<string[]>(jsonParaStrings);
        TestObjext paraObjectD = JSON.Deserialize<TestObjext>(jsonParaObject);
        ...
    }
  10. #10
    Quote Originally Posted by Daniil View Post
    I forgot that parameters of a controller action are not deserialized automatically.

    Please deserialize manually.

    And you don't need to call Ext.encode() manually, it is done automatically.

    Example

    params  : {
        jsonParaStrings: getParametersStringArray(),
        jsonParaObject: getParametersObject()
    }
    
    public ActionResult Download(string jsonParaStrings, string jsonParaObject)
    {
        string[] paraStringsD = JSON.Deserialize<string[]>(jsonParaStrings);
        TestObjext paraObjectD = JSON.Deserialize<TestObjext>(jsonParaObject);
        ...
    }
    Thank you Daniil, this time it's working now.

    One more question is about the return type from controller's method

    In the javascript DirectMethod I want to have some process when download successful. What should I return?
    the javascript method is:
           var download = function () {
                Ext.net.DirectMethod.request({
                    url           : "/Test/Download/",
                    isUpload      : true,
                    formProxyArg  : "fileform",
                    cleanRequest  : true,
                    params        : {
                        paraStrings: getParametersStringArray(),
                        paraObject: getParametersObject()
                    },
                    success: function (result) {
                    //....Procession//
                }
                });
            };
    the current controller's method is
    public ActionResult Download(string[] paraStrings, TestObjext paraObject)
    {
        var path = Server.MapPath(paraStrings[2]);
        var file = new FileInfo(path);
        if (file.Exists && paraObject!=null)
        {
            return File(url, "application/octetstream", "test.xls");
        }
        
        AjaxResult r = new AjaxResult(string.Format("alert('{0} is not exists')", path));
        r.IsUpload = true;
        return r;
    }
    The success in javascript method will never reach. I think that's might be the return type from controller's method is not correct.
Page 1 of 2 12 LastLast

Similar Threads

  1. [CLOSED] [1.0] MVC Download file
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Aug 02, 2010, 11:10 AM
  2. [CLOSED] Download a file in an MVC app
    By Stefanaccio in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jul 02, 2010, 9:01 PM
  3. [CLOSED] Ajax File Download - Handling an Error
    By rcaunt in forum 1.x Legacy Premium Help
    Replies: 14
    Last Post: Apr 12, 2010, 10:01 AM
  4. [CLOSED] [1.0] File download in MVC
    By tdracz in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 23, 2009, 4:43 PM
  5. File download
    By speedstepmem4 in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Dec 05, 2008, 7:22 AM

Tags for this Thread

Posting Permissions