user1719891
user1719891

Reputation: 29

File upload from one domain to another domain

I had created one website which has two modules,

  1. ADMIN
  2. USER

They are hosted on different domains. Now when user open its domain suppose its abc.com and can register their company and also upload photo from there and uploaded photo will go in Company_Logo FOLDER. Now suppose ADMIN's domain is xyz.com . now i want that ADMIN open its xyz.com and can see the photo uploaded from abc.com now i want like ADMIN means from xyz.com can change that uploaded photo to abc.com which is in Company_Logo FOLDER.

In short photo uploded from User side which is on abc.com and replace from ADMIN side which is on xyz.com so how can i do that

Upvotes: 2

Views: 2146

Answers (4)

Murtuza Kabul
Murtuza Kabul

Reputation: 6514

You have two options.

  • If both of your sites are hosted in the same machine or a shared hosting environment, chances are there that your site can access the other directories. In that case you will be easily able to place the images in desired folder.

  • Now the second case, where one of your site does not have access to the folder of another site, - it is rather complicated. You will have to create a proxy where by the admin site will accept the image and in turn it will put it in the main site folder. I do not recommend this though.

Upvotes: 1

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Okay, this can be done but you'll need to use an HttpHandler. You can find a good example here, but I'll spell out the important parts. I cannot feasibly write the entire handler for you here.

First, let's build a class in the web project and call it ImageHandler ...

public class ImageHandler : IHttpHandler
{
}

... next let's implement the interface ...

public bool IsReusable
{
    get { return false; }
}

public void ProcessRequest(HttpContext context)
{
    // find out what we're trying to do first
    string method = context.Request.HttpMethod;

    switch (method)
    {
        case "GET":
            // read the query string for the document name or ID

            // read the file in from the shared folder

            // write those bytes to the response, ensuring to set the Reponse.ContentType
            // and also remember to issue Reponse.Clear()

            break;
        case "PUT":
            // read the Headers from the Request to get the byte[] of the file to CREATE

            // write those bytes to disk

            // construct a 200 response

            break;
        case "POST":
            // read the Headers from the Request to get the byte[] of the file to UPDATE

            // write those bytes to disk

            // construct a 200 response

            break;
        case "DELETE":
            // read the Headers from the Request to get the byte[] of the file to DELETE

            // write those bytes to disk

            // construct a 200 response

            break;
    }
}

... finally we need to setup the handler in the web.config ...

<configuration>
   <system.web>
      <httpHandlers>
         <!-- remember that you need to replace the {YourNamespace} with your fully qualified -->
         <!-- namespace and you need to replace {YourAssemblyName} with your assembly name    -->
         <!-- EXCLUDING the .dll                                                              -->
         <add verb="*" path="*/images/*" type="{YourNamespace}.ImageHandler, {YourAssemblyName}" />
      </httpHandlers>
   </system.web>
</configuration>

Finally, something you're also going to want to do is pass in some kind of session key that can be validated when you get into the handler because otherwise this is open to everbody. It wouldn't matter if you didn't need the PUT, POST and DELETE verbs, but you do.

Technically you wouldn't need to check the session key on GET if you didn't care that everybody could access the GET, but you gotta check it on the others.

Upvotes: 1

Talha Ashfaque
Talha Ashfaque

Reputation: 4062

You can do this in 2 steps:

1) Upload image to your server using standard File Upload mechanism

2) Use HttpWebRequest class to upload image to different server on server-side right after original upload. Please refer to this article: Upload files with HTTPWebrequest (multipart/form-data)

see this for reference: http://forums.asp.net/t/1726911.aspx/1

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151588

So you have two different sites, hosted on different domains and perhaps even different servers, and you want site A to notify site B when some file has been uploaded. You then want to be able to alter that file on site A from site B.

Seems to me you need to create some sort of API on site A, that lets users (admins) from site B check recently uploaded files and also lets them overwrite it.

Upvotes: 1

Related Questions