Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

file download from webmethod..its show no result

$
0
0

hi..

i am using webmethod to download my file.my webmethod code is..

[System.Web.Services.WebMethod()]
    public static bool downloadfile(string path)
    {

        try
        {
            string str = HttpContext.Current.Server.MapPath("~/Files") + "\\" + (HttpContext.Current.Session["ProjectId"]) + "\\" + path;
            dowloadfile(str);
        }
        catch (Exception ex)
        {
            return false;
        }

        
        return true;
    }

    private static void dowloadfile(string path)
    {
        // Create New instance of FileInfo class to get the properties of the file being downloaded
        FileInfo myfile = new FileInfo(path);

        // Checking if file exists
        if (myfile.Exists)
        {
            System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
            // Clear the content of the response
            Response.ClearContent();

            // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
            Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

            
            // Add the file size into the response header
            Response.AddHeader("Content-Length", myfile.Length.ToString());

            // Set the ContentType
            Response.ContentType = "image/gif;

            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
            Response.TransmitFile(myfile.FullName);
            
            Response.Flush();
            // End the response
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

    }

all things return true..but the download is no happend..

this is my ajax code

function download(path) {$.ajax({
                        type: "POST",
                        url: "viewprojectdetails.aspx/downloadfile",
                        data: '{path:"' + path + '"}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (r) {

                            alert(r.d);

                        }

                    });
                
                }

download button i send from code behind with my file name(path).an a onclick function

<input type='Button' ID='btnDowmLoad'  onclick='download(\"" + dr["Path"].ToString() + "\")' value='Download' />"

but i dont know where is my problem..why the file is not downloaded ..pls help me....



Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>