user1618825
user1618825

Reputation:

MVC Download File from Remote Server

I am trying to download file from Remote Server but I am running up with errors.

public ActionResult Download()
{
    var a = new Uri("file://<<REMOTE_SERVER>>/<<Folder>>/Test.csv");
    return File(a, "application/csv", "test.csv");
}

Error:

file://<<REMOTE_SERVER>>/<<Folder>>/Test.csv is not a valid virtual path.

Upvotes: 1

Views: 2229

Answers (3)

Ritesh Kumar
Ritesh Kumar

Reputation: 1

[HttpGet]
public EmptyResult DownloadAttachment()        
{
    var filePath = "http://img.local.com/Images/Customers/15364053/Inv_LMEM1LA6543_1.png";

    using (WebClient client = new WebClient())
    {
        byte[] imageData = client.DownloadData(filePath);

        string contentType = "";
        if (fileName.ToLower().Contains(".png"))
        {
            contentType = "Images/png";
        }
        else if (fileName.ToLower().Contains(".jpg"))
        {
            contentType = "Images/jpg";
        }
        else if (fileName.ToLower().Contains(".jpeg"))
        {
            contentType = "Images/jpeg";
        }
        else if (fileName.ToLower().Contains(".pdf"))
        {
            contentType = "Images/pdf";
        }
        else if (fileName.ToLower().Contains(".tiff"))
        {
            contentType = "Images/tiff";
        }

        Response.ContentType = contentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.ContentType = "image/JPEG";
        Response.OutputStream.Write(imageData, 0, imageData.Length);
        Response.End();

    }
    return new EmptyResult();
}

Upvotes: 0

Andrew Kilburn
Andrew Kilburn

Reputation: 2251

using System.Security.Principal;          

using (new Impersonator("myUsername", "myDomainname", "myPassword"))
          {
             return File(@"<<REMOTE_SERVER>>\<<Folder>>\Test.csv", "application/csv", "test.csv");
          }

If you add the @ sign it will disable any escape characters. Besides you only have one / in the middle of your path which is why it isn't valid.

Look here for more info: https://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.110).aspx

Upvotes: 1

mxmissile
mxmissile

Reputation: 11681

Try using a direct path:

var a = "\\\<<REMOTE_SERVER>>\\<<Folder>>\\Test.csv";
return File(a, "application/csv", "test.csv");

Upvotes: 0

Related Questions