Created
March 5, 2009 00:33
-
-
Save Benrnz/74112 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///<summary>Upload a file using the Asp.Net 2.0 Upload control. | |
///Requires a button to which this handler responds to Onclick. | |
///</summary> | |
protected void UploadBtn_Click(object sender, EventArgs e) { | |
try { | |
if (FileUpload1.HasFile) { | |
bool failure = false; | |
if (!FileUpload1.FileName.ToLowerInvariant().EndsWith(".xml")) { | |
failure = true; | |
} | |
StreamReader reader = new StreamReader(FileUpload1.FileContent); | |
string xmlContent = reader.ReadToEnd(); | |
if (!xmlContent.TrimStart().StartsWith("<?xml")) { | |
failure = true; | |
} | |
if (!failure) { | |
Label1.Text = "Only xml files can be uploaded."; | |
} else { | |
FileUpload1.SaveAs(this.Server.MapPath("TestData.xml")); | |
Label1.Text = "File Uploaded: " + FileUpload1.FileName; | |
} | |
} else { | |
Label1.Text = "No File to Uploaded."; | |
} | |
} catch (Exception ex) { | |
Label1.Text = | |
"An error occured while uploading or parsing the xml file. Only xml files can be uploaded."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment