'Web API Upload File to Different Directory

is it possible to upload files using Web API to a different directory and not just on the App Root Folder? Either folder on the same server outside App Root Folder or another server.

I need to upload files using Web API to another directory with more space.

public HttpResponseMessage Post()
    {
        try
        {
            string mydir = "~/Files/"; //-- APP ROOT FOLDER --//

            if (!(Directory.Exists(HttpContext.Current.Server.MapPath(mydir))))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(mydir));
            }

            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Files.Count > 0)
            {
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];
                    string filename = string.Concat(GetActualFilename(postedFile.FileName), GetExtension(postedFile.FileName));

                    var filePath = HttpContext.Current.Server.MapPath(mydir + "/" + filename);
                    postedFile.SaveAs(filePath);
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.OK, "No file attached/processed.");
            }

            return Request.CreateResponse(HttpStatusCode.Created, "Successfully uploaded file(s).");
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
        }
    }

    private static string GetActualFilename(string filename)
    {
        for (int i = filename.Length - 1; i >= 0; i--)
        {
            if (filename[i] == '.')
            {
                return filename.Substring(0, i);
            }
        }
        return filename;
    }

    private static string GetExtension(string filename)
    {
        for (int i = filename.Length - 1; i >= 0; i--)
        {
            if (filename[i] == '.')
            {
                return filename.Substring(i);
            }
        }
        return "";
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source