tags:

views:

35

answers:

1

I have a controller that needs to redirect after receiving a file. I have saved the file successfully on the server side. Now, the only things that is bogging me down is how do I redirect to another site while sending the uploaded file that was saved on the server? Any tips? I am desparate.

OK so here it is, first I save the file on serverB:

file.SaveAs(Server.MapPath("~/ImageCache/") + file.FileName);
WebClient client = new WebClient();

Then I do the post:

byte[] data;
client.Headers.Set(HttpRequestHeader.ContentType, "image/jpeg");
data = client.UploadFile("http://hostA.com/Search/", "POST", Server.MapPath("~/ImageCache/") + file.FileName); 
return Redirect( WHAT DO I WRITE HERE??);

Need to get to the place where I find the other service showing me the page when it has received the file.

+2  A: 

How are you uploading the file? If this is the usual case of an <input type="file" />, you can just return Redirect("new url"); within your action.

Edit:

If you want to relay this to another web service, you don't need to redirect. There should be some sort of upload method defined in the webservice (including what type of webservice would help). You should be able to call that like you would any other webservice method, probably specifying the FileContents byte[] as a parameter.

Ian Henry
Well I did try the Rediret("new url"), but on the other end another webservice that is to recieve the file, keeps getting null while the file type it must receive is HttpPostedFileBase. The url looks like this http://ServiceA/Search?file=thefilesentfromServiceB
Wajih
Ah, I very much misunderstood your question.
Ian Henry
@wajih If you want to call a webservice in a different server, you don't need to redirect the user. Just add a reference to the service in your project, and call the webservice within the method in the controller.
salgiza
@salgiza, I can't do that. I am not allowed to do so.
Wajih
@Henry, Ok so I just tried out the WebClient, the problem now I get is that the connection gets forcibly closed when I try to upload to the specific service. Now why is this happening?
Wajih
@Ian. Just out of curiosity after reading your edited answer. Are you sure that would work? I would have thought that redirect would always use a GET method instead of POST for the destination address, thus making it impossible to send the file (AFAIK you can only send files using POST).
salgiza
@Wajih - It's impossible to say without more information. I suggest editing your question with all the relevant code and details. @salgiza - I'm not sure what you mean; my edit said that redirecting was not the way to go. Reading your comment, I think we said pretty much the same thing.
Ian Henry
OK sore here it is, first I save the file on serverBfile.SaveAs(Server.MapPath("~/ImageCache/")+file.FileName);WebClient client = new WebClient();byte[] data;Then I do the postclient.Headers.Set(HttpRequestHeader.ContentType, "image/jpeg");data = client.UploadFile("http://hostA.com/Search/", "POST", Server.MapPath("~/ImageCache/") + file.FileName);return Redirect( WHAT DO I WRITE HERE??); Need to get to the place where I find the other service showing me the page when it has received the file.
Wajih
@Wajih - Edit your question with the code. This is not a comment on this answer; it's additional question details. Also, that's completely unreadable in a comment.
Ian Henry