I want to write ASP.NET code showing the use of FileUpload

I want to write ASP.NET code showing the use of FileUpload



I have written a code in which when the user clicks upload button the word file should be saved and redirect to the 2nd page, the word file is saving in the given path, but it is not printing any output(it is showing me blank page)



1st page.aspx


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class pg_upload : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)



protected void Button1_Click(object sender, EventArgs e)

string fname = FileUpload1.FileName;

if (FileUpload1.HasFiles)

Label1.Text = "Selected file uploaded";


else
Label1.Text += "Select a file";
return;

int flength=FileUpload1.PostedFile.ContentLength;
Int32 a = flength * 1024;
if (flength > a)

Label1.Text += "File Out Of Range";

if (FileUpload1.PostedFile.ContentType != "application/msword")

Label1.Text += "Upload word files only";

FileUpload1.SaveAs(Server.MapPath("~")+"/Myfolder/"+
FileUpload1.FileName);
Response.Redirect("pg_viewUploads.aspx");





My 2nd page.aspx


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class pg_viewUploads : System.Web.UI.Page

protected void Page_Load(object sender, EventArgs e)

string fpath=Server.MapPath("~")+"/Myfolder";
string files=Directory.GetFiles(fpath);
string name=new string[files.Length];
FileInfo fi;
int i=0;
foreach (string s in files)

fi = new FileInfo(s);
name[i] = "/Myfolder/" + fi.Name;
i++;


StringBuilder sb = new StringBuilder();
sb.Append("<table>");
i = 0;
sb.Append("<tr>");
for (int j = 1; j < 3; j++)

sb.Append("<tr>");
for (int k = 1; k <3; k++)

if (i > files.Length-1)
return;
sb.Append("<a href="+ name[i] + ">" +name[i]+"</a>");
i++;


sb.Append("</tr>");


sb.Append("</tr>");
sb.Append("</table>");
Label b = new Label();
b.Text = sb.ToString();
b.Visible = true;
PlaceHolder1.Controls.Add(b);






This pic will give you detail idea of how to code should run
Please provide solution.any help will be appreciated, thanks in advance






What do you mean by "it is not printing any output"?

– Dan Friedman
Sep 10 '18 at 0:42






means it is showing me blank page

– semicolon
Sep 10 '18 at 1:28






So assuming that the file is uploaded and that you can successfully open it, you actually don't need any help uploading the file. You need help displaying a list of file names, right?

– Dan Friedman
Sep 10 '18 at 3:51




1 Answer
1



The first issue I see is that you are embedding <tr> tags in a <tr> tag. This is not allowed in HTML.


<tr>


<tr>



The second issue I see is that you are looping over j and k for no obvious reason. Looping over the files array should be enough.


j


k


files






I have used for loop to print it in a table

– semicolon
Sep 10 '18 at 4:03






thanks i removed the extra <tr> tag

– semicolon
Sep 10 '18 at 4:07



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)