Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
But while download file, the program continue execution.

C#
//Create a WebRequest to get the file
            FileWebRequest fileReq = (FileWebRequest)FileWebRequest.Create(this.CaminhoCompleto);

            //Create a response for this request
            FileWebResponse fileResp = (FileWebResponse)fileReq.GetResponse();

            if (fileReq.ContentLength > 0)
                fileResp.ContentLength = fileReq.ContentLength;

            //Get the Stream returned from the response
            stream = fileResp.GetResponseStream();


            // prepare the response to the client. resp is the client Response
            HttpResponse resp = HttpContext.Current.Response;

            

            //ERRO!!!
            teste(stream, resp, fileResp);


            string nomeArq = fileResp.ResponseUri.Segments[(fileResp.ResponseUri.Segments.Length) - 1];

            //Indicate the type of data being sent
            resp.ContentType = "application/octet-stream";

            //Name the file 
            resp.AddHeader("Content-Disposition", "attachment; filename=\"" + nomeArq + "\"");
            resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

            //await stream.CopyToAsync(resp.OutputStream);
Posted
Comments
ZurdoDev 6-Jun-14 10:54am    
Call a webservice from JavaScript (jquery .ajax) and do it that way.
Prasad Khandekar 6-Jun-14 11:42am    
Hello Yitzhak,

From your web client use Window.Open(DOWNLOAD_URL, "_blank"). Where DOWNLOAD_URL is the URL for page which servers the content (the page in which the above mentioned code resides).

Regards,
Yitzhak Stone 6-Jun-14 13:04pm    
Unfortunately this does not give way, because the files need permission to be downloaded, so it can not be by URL.
Prasad Khandekar 9-Jun-14 2:05am    
Well the download page is going to perform that check isn't it. The user must be first authenticating to the website.

Regards,
Yitzhak Stone 9-Jun-14 6:59am    
Once you have the URL, the entire audience will access it, logged or not logged into the site.

1 solution

Review my code below:

protected void btnDowmLoad_Click(object sender, EventArgs e)
{
    try
    {
        string strURL=txtFileName.Text;
        WebClient req=new WebClient();
        HttpResponse response = HttpContext.Current.Response;
        response.Clear();
        response.ClearContent();
        response.ClearHeaders();
        response.Buffer= true;
        response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strURL) + "\"");
        byte[] data=req.DownloadData(Server.MapPath(strURL));
        response.BinaryWrite(data);
        response.End();
    }
    catch(Exception ex)
    {
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900