If you just need to upload a file there is 2 simeple ways, one is with the AJAX file upload, these let you upload multiple files and have some nice java forms that looks fine, in the personal I dont like it to much I prefere the old stile, then the thing is if you are going to upload a single file o multiple files at the same time, and Im looking you are adding validator for only xls, xlsx files
, that make me think in if also you will need to thow the results in to a DB or is just matter to store the file.
If ts just matter to store the file.
Single File.
<div id="Div1" runat="server">
<br />
<asp:fileupload id="FileUpload3" runat="server" enabled="true" xmlns:asp="#unknown" />
<asp:button id="Button1" runat="server" text="Upload" onclick="btnUpload_Click" xmlns:asp="#unknown" />
<asp:label id="Label1" runat="server" text="" xmlns:asp="#unknown" />
<br />
<asp:label id="Label6" runat="server" text="" xmlns:asp="#unknown" />
</div>
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = Server.HtmlEncode(FileUpload1.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower();
if (Extension != ".xls" && Extension != ".xlsx")
{
Response.Write("<script>alert('Please select a Excel spreadsheet to import!');</script>");
return;
}
string abc = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
string strUploadFileName = "~/TradeCompliance/Functional/Files_Archive/" + abc + " " + DateTime.Now.ToString("yyyyMMddHHmmss") + Extension;
string FilePath = Server.MapPath(strUploadFileName);
FileUpload1.SaveAs(FilePath);
}
}
If multiple files needs to be stored at the same time then I use this (Check that in name you can sort the type of valid files to search for upload and the accept the type of file application works for xls 97 up to last vertion):
<br />
<input type="file" multiple="multiple" runat="server" width="300px" height="300px" aria-dropeffect="move" aria-multiline="true">
aria-multiselectable="true" draggable="true" name="pdf" id="File1" accept="application/pdf" /><asp:label id="Label1" text="UploadComplete" runat="server" visible="false" xmlns:asp="#unknown" /><br />
<asp:button id="Button2" runat="server" text="UPLOAD" onclick="Button1_Click" xmlns:asp="#unknown" />
<br />
<asp:label id="Label2" runat="server" xmlns:asp="#unknown" />
<br />
<hr />
<div>
<asp:literal id="Literal1" runat="server" xmlns:asp="#unknown" />
</div>
<br />
</input>
protected void Button1_Click(object sender, EventArgs e)
{
string filepath = Server.MapPath("\\Logistics\\ArchiveFiles\\");
string contentType = "";
string filename = "";
string HAWB_BOL = "";
string tempPath = "";
int filecnt = 0;
HttpFileCollection uploadedFiles = Request.Files;
Span1.Text = string.Empty;
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
contentType = userPostedFile.ContentType;
filename = Path.GetFileName(userPostedFile.FileName);
HAWB_BOL = filename.Replace(".pdf", "");
filecnt = AD(HAWB_BOL);
tempPath = filepath + filename;
if(filecnt < 1)
{
try
{
if (userPostedFile.ContentLength > 0)
{
userPostedFile.SaveAs(tempPath);
}
}
catch (Exception Ex)
{
Span1.Text += "Error: <br>" + Ex.Message;
}
}else
{
userPostedFile.SaveAs(tempPath);
File.Delete(tempPath);
}
}
}
</br>
I just delete dome parts of the code as I save in the multiple scenario to a DB to get the files available against a file, but basically with some minor changes you should be able to implement any of these.