public void ButtonUploadFile_Click(object sender, System.EventArgs e)
{
try
{
if (FileUploadExcel.HasFile)
{
string strFileType = System.IO.Path.GetExtension(FileUploadExcel.FileName).ToString().ToLower();
if (strFileType.Trim() == ".xlsx" || strFileType.Trim() == ".xls")
{
tempValue.Text = FileUploadExcel.PostedFile.FileName.ToString();
tempFileName.Text = FileUploadExcel.FileName.ToString();
tempType.Text = FileUploadExcel.PostedFile.ContentType.ToString();
tempSize.Text = FileUploadExcel.PostedFile.ContentLength.ToString();
tempBytes.Text = FileUploadExcel.FileBytes.ToString();
SPWeb spWeb = SPContext.Current.Web;
spWeb.AllowUnsafeUpdates = true;
SPDocumentLibrary spDocLib = spWeb.Lists["Archive"] as SPDocumentLibrary;
//Get file extension ( *.xls OR *.xlsx OR whatever files )
string fileExtension = FileUploadExcel.FileName.Substring(FileUploadExcel.FileName.IndexOf("."));
byte[] fileBytes = FileUploadExcel.FileBytes;
string strURL = spDocLib.RootFolder.Url + "/" + tempFileName.Text;
SPFile spFile = spDocLib.RootFolder.Files.Add(strURL, fileBytes, true);
spFile.Update();
spWeb.AllowUnsafeUpdates = false;
LabelUpload.Text = tempValue.Text;
lblMessage.Visible = true;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "<B>File Name : </B>" + FileUploadExcel.PostedFile.FileName + "<BR><B>File Type : </B>" + FileUploadExcel.PostedFile.ContentType + "<BR><B>File Size : </B>" + FileUploadExcel.PostedFile.ContentLength + " kb<BR><BR>File successfully uploaded to server.";
}
else
{
PanelView.Visible = false;
LabelUpload.Visible = false;
lblMessage.Text = "Please select Excel (.xls) document only.";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
else
{
PanelView.Visible = false;
lblMessage.Visible = false;
LabelUpload.Visible = true;
LabelUpload.ForeColor = System.Drawing.Color.Red;
LabelUpload.Text = "Please select a file to upload.";
}
}
catch (Exception ex)
{
LabelUpload.Visible = true;
LabelUpload.Text = "Error: " + ex.Message.ToString();
}
}