开发者

Why Response.Write behavior varies in the given scenario?

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/We开发者_StackOverflowb/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.


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: How can I upload files asynchronously?


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.


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.


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.


I would suggest changing CrossPage.aspx into an .ashx HttpHandler. The Page.IsPostBack may not be working correctly because it is expecting ViewState and other hidden ASP.net form fields that tell it that it's a post back from an ASP form. You also don't need to go through the whole Page life cycle functionality that ASP.net Webforms goes through.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜