简体   繁体   English

上传文件后如何重定向?

[英]How to redirect after File upload?

I have a File.aspx form where I display all the files. 我有一个File.aspx表单,在其中显示所有文件。

When I upload a file I am using a upload.aspx file to run c# code to upload the file. 当我上传文件时,我正在使用upload.aspx文件来运行c#代码来上传文件。

The thing is that at the end , I want to refresh the page automatically to display the new file upload but apparently it's not redirecting even if all the parameters are good. 问题是,最后,我想自动刷新页面以显示新文件上传,但是即使所有参数都很好,也显然不会重定向。

This is the code I use 这是我使用的代码

        HttpContext postedContext = HttpContext.Current;
        string param = Request.UrlReferrer.Query;
        string param2 = Request.UrlReferrer.Query;
        var url = HttpUtility.ParseQueryString(param).Get("projectName");
        var url2 = HttpUtility.ParseQueryString(param2).Get("projectId");
        HttpPostedFile file = postedContext.Request.Files[0];
        string name = file.FileName;
        byte[] binaryWriteArray = new
        byte[file.InputStream.Length];
        file.InputStream.Read(binaryWriteArray, 0,
        (int)file.InputStream.Length);
        //FileStream objfilestream = new FileStream(Server.MapPath("~\\" + "\\Files\\" + url + "\\" + name), FileMode.Create, FileAccess.ReadWrite);
        FileStream objfilestream = new FileStream(("C:\\inetpub\\wwwroot\\Clientportal\\Files\\" + url + "\\" + name), FileMode.Create, FileAccess.ReadWrite);
        objfilestream.Write(binaryWriteArray, 0, binaryWriteArray.Length);

        objfilestream.Close();
        string[][] JaggedArray = new string[1][];
        JaggedArray[0] = new string[] { "File was uploaded successfully" };
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(JaggedArray);
        Response.Write(strJSON);

        Response.Redirect(string.Format("Files.aspx?projectId={0}&projectName={1}", url2, url));

Any ideas ? 有任何想法吗 ? I'm a bit stuck because I use this response.redirect everywhere and it works only when the code is in the same code file... 我有点卡住了,因为我在任何地方都使用了这个response.redirect,并且仅当代码在同一代码文件中时才有效...

Maybe it's because of the fact that I try to redirect from another code file ? 也许是因为我尝试从另一个代码文件重定向?

I would attempt to do reload on client side by using javascript: 我会尝试使用javascript在客户端重新加载:

window.location.reload()

if the JSON you get as a response contains message "File was uploaded successfully" If you are not using ajax for submitting the file you don't need to write json. 如果您作为响应获取的JSON包含消息“文件已成功上传”,则如果您不使用ajax提交文件,则无需编写json。 It's enough to redirect to the suitable url that contains a parameter that identifies status of file upload operation. 只需重定向到包含标识文件上传操作状态的参数的适当url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM