views:

284

answers:

4

Hello All,

When i POST the page using the following code, the Response.write("Hey") doesn't write the content ("Hey") to the parent page

<form method="post" name="upload" enctype="multipart/form-data"
action="http://localhost:2518/Web/CrossPage.aspx" >
<input type="file" name="filename" />
<input type="submit" value="Upload Data File" name="cmdSubmit" />
</form>

But When i use following code , and POST the data, the Response.write("Hey") can be obtained in the parent page

 HttpWebRequest requestToSender = (HttpWebRequest)WebRequest.Create("http://localhost:2518/Web/CrossPage.aspx");
 requestToSender.Method = "POST";
 requestToSender.ContentType = "multipart/form-data";

 HttpWebResponse responseFromSender = (HttpWebResponse)requestToSender.GetResponse();
 string fromSender = string.Empty;

 using (StreamReader responseReader = new StreamReader(responseFromSender.GetResponseStream()))
    {
        fromSender = responseReader.ReadToEnd();
    }

In the CrossPage.aspx i have the following code

 if (!Page.IsPostBack)
    {
        NameValueCollection postPageCollection = Request.Form;

        foreach (string name in postPageCollection.AllKeys)
        {
            Response.Write(name + " " + postPageCollection[name]);
        }

        HttpFileCollection postCollection = Request.Files;
        foreach (string name in postCollection.AllKeys)
        {
            HttpPostedFile aFile = postCollection[name];
            aFile.SaveAs(Server.MapPath(".") + "/" + Path.GetFileName(aFile.FileName));
        }

        Response.Write("Hey");
    }

I don't have any code in the Page_Load event of parent page.?

What could be the cause? I need to write the "hey" to the Parent page using the first scenario. Both the application are of different domain.

Edit: "Hey" would be from the CrossPage.aspx. I need to write this back to the Parent Page

when i post using the form action, after processing the Page_Load() event in CrossPage.aspx, the URL points to "http://localhost:2518/Web/CrossPage.aspx" which means the application is still in the CrossPage.aspx and didn't move to parent page.

A: 

I say your aFile.SaveAs(Server.MapPath(".") + "/".... is throwing an exception. try commenting it out and testing it.

UPDATE:

I'm guessing it works from a HttpWebRequest because there is no file being posted therefore the file loop is skipped. when posting from the HTML you have a file input so your file loop is getting used and resulting in the save logic being executed. so again, that leads me to think it's your save logic

Also,I think you have a try catch statement wrapped around all this that is catching exception so u have no idea what's going wrong. If that's the case, never do that. You rarely ever want to catch exception.

After quick glance at ur save logic, replace "/" with @"\". Server.MapPath(".") returns the path using back slashes not forward slashes.

used2could
Sorry. It works fine. I don't have any issues with SaveAs
Sri Kumar
strange, i ran the code on my end and it does as expected with the Save logic removed.
used2could
can i know what kind of exception it throws?
Sri Kumar
I just commented out the save logic because i didn't take the time to setup any files to test with. There was no exception. It all worked with the save logic commented out.
used2could
I'm guessing it works from a HttpWebRequest because there is no file being posted therefore the file loop is skipped. when posting from the HTML u have a file input so your file loop is getting used and resulting in the save logic to be executed. so again, that leads me to think it's ur save logic.
used2could
Is this the exact code you're using? or did you simplify it to post here? i'm guessing you have a try catch statement wrapped around all this that is catching "exception" so u have no idea what's going wrong. If that's the case, never do that. You rarely ever want to catch "exception"
used2could
Nope i don't have any try catch and the save work PERFECTLY. I have finished the complete feature using HTTPWebRequest and I still use the same SaveAs. It looks really strange :)
Sri Kumar
As a GOOD practice, during the initial period of development I don't use try catch unless it is highly essential. :)
Sri Kumar
Still I user this line in my code... aFile.SaveAs(Server.MapPath(".") + "/" + Path.GetFileName(aFile.FileName));
Sri Kumar
A: 

Probably it is because you have the code in an if (!Page.IsPostBack) block? this code will be executed only the page is not loaded on a post-back.

(HttpWebResponse)requestToSender.GetResponse(); will trigger a GET request, that's why your code is working when you call Crosspage.aspx using that code.

NimsDotNet
Ofcourse it should do only on the Page Load as i don't have any post page events in the CrossPage.aspx page
Sri Kumar
The "Get" in .GetResponse() does not imply an HTTP GET.requestToSender.Method = "POST"; makes it a POST
Matt
A: 

You are treating the page like a service. E.G. Start at ParentPage.aspx > pass data to ServicePage.aspx for processing > write response back to ParentPage.aspx for display.

You got it to work with C# by passing the duty back to the server, where state can easily be maintained while crossing page boundries. It's not so simple when you try to solve the problem without C#. This isn't a Winform app. As NimsDotNet pointed out, changing the method to "get" will get you closer, but you will get redirected to CrossPage.aspx and lose the calling page.

You said you are on "different domains". By this I think you mean your using two different IIS servers. The C# solution should still work in this scenerio, just as you've shown. Just add a Response.Write() of the fromSender object. There's nothing you've told us that makes this not technically possible. Still, if you want a client side solution you could use javascript to make the get request without getting redirected.

This post shows you how to make a get request with JQuery.

P.Brian.Mackey
+1  A: 

You almost hit on the reason yourself:

when i post using the form action, after processing the Page_Load() event in CrossPage.aspx, the URL points to "http://localhost:2518/Web/CrossPage.aspx" which means the application is still in the CrossPage.aspx and didn't move to parent page.

Your form takes the user to CrossPage.aspx, so the parent page is gone, now the previous page in the user's history.

It sounds like you are trying to do some sort of asynchronous file upload. Try looking for AJAX file upload examples, something like this: http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery

batwad