Chong
Chong

Reputation: 604

To save files to IIS virtual directory from WCF?

I am using WCF service in my application.All services are hosted as window service. I have one virtual directory in IIS to save user file (may be PDF or TXT). I want to create a particular folder for each user and keep their information file in that folder. How can I create a folder and upload file to IIS via WCF?

For example: the virtual directory path is http://10.10.10.1/TempUserFolder/

I want to create a folder for UserID = 1 like http://10.10.10.1/TempFolder/UserID1/. And then save his information file to that folder, http://10.10.10.1/TempFolder/UserID1/Info.pdf.

I got a error message URI formats are not supported when I use System.IO. Please guide me in right way. I really appreciate your help. I am using VS200

Upvotes: 2

Views: 8396

Answers (3)

competent_tech
competent_tech

Reputation: 44931

To get the physical directory name, use

Server.MapPath('TempFolder/UserID1');

Update

As pointed out in the comments, the above solution will only work when using asp.net compatibility in an IIS-hosted solution.

If you don't want to hard-code the directory, for example if this is deployed to different servers, you can get the directory of the WCF assembly, then figure out the relative relation of the desired directories from there.

For example, assuming that the DLL is in the bin directory and the temp directory is 1 level up, the following code should work:

string sDirectory = System.Reflection.Assembly.GetExecutingAssembly.Location;
sDirectory = System.IO.Path.Combine(sDirectory, "..\TempFolder\UserID1");

Upvotes: 0

Anand
Anand

Reputation: 757

This will give a virtual directory of current application.With this you can append your created folder name

    public string GetIISPath()
    {
        string urlscheme = System.Web.HttpContext.Current.Request.Url.Scheme;
        string host = System.Web.HttpContext.Current.Request.Url.Host;
        int port = System.Web.HttpContext.Current.Request.Url.Port;         

        //Ignore Http Port
        if (port != 80)
            host = host + ":" + port;
        string vPath = urlscheme + "://" + host + "/";
        return vPath;
    }

Upvotes: 0

Brijesh Mishra
Brijesh Mishra

Reputation: 2748

If your windows service is on same IIS host server, you can very well use absolute path, say if http://10.10.10.1/TempFolder/ maps to c:/TempFolder/, in WCF windowsservice when recieving file create new folder in c:/TempFolder/ and store file there, and if its on different machine you can create network share and then create folder and copy files to that location.

You can find many article on web to demonstrate how to upload file via wcf

http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/

Upvotes: 2

Related Questions