Exemplo n.º 1
0
        protected void UploadFile(FileUpload fuBulkUpload, string _filename)
        {
            // Delay for Progress Bar
            Thread.Sleep(5000);

            #region Add Item Programmatically
            DirectoryInfo dir = null;
            string        extension, fileName = string.Empty;
            string        path      = string.Empty;
            string        tableName = string.Empty;
            string        getError  = string.Empty;
            DataSet       dataSet   = new DataSet();
            try
            {
                if (fuBulkUpload.HasFile)
                {
                    string folderName = Guid.NewGuid().ToString();

                    string        folderpath        = Server.MapPath("~/sitecore/admin/template/" + folderName + "/");
                    string[]      templatePathFiles = Directory.GetFiles(Server.MapPath("~/sitecore/admin/template/"), "*.xml", SearchOption.TopDirectoryOnly);
                    List <string> templateFiles     = new List <string>();
                    foreach (string template in templatePathFiles)
                    {
                        templateFiles.Add(Path.GetFileName(template).Substring(0, Path.GetFileName(template).LastIndexOf(".")));
                    }
                    ServerMapPath = folderpath;
                    dir           = Directory.CreateDirectory(folderpath);
                    extension     = Path.GetExtension(fuBulkUpload.FileName);
                    fileName      = fuBulkUpload.FileName.Substring(0, fuBulkUpload.FileName.LastIndexOf('.'));
                    //if (fuBulkUpload.PostedFile.ContentLength > (maxFile * 1024))
                    //    throw new Exception(string.Format("File size more than {0}MB, please upload file less than {1}MB.", maxFile, maxFile));
                    if (extension != ".csv")
                    {
                        throw new FormatException("Wrong File Format.");
                    }
                    if (_filename != fileName)
                    {
                        throw new Exception("Wrong File Upload. File Upload using filename " + _filename + ".csv or Try another File Upload..");
                    }
                    if (!templateFiles.Contains(fileName))
                    {
                        throw new Exception("Wrong File Name. No template name equals with this file name");
                    }
                    path = folderpath + fuBulkUpload.FileName;
                    fuBulkUpload.SaveAs(path);

                    Data = LibraryHelpers.ConvertCSVToTable(path);
                    UploadHelpers.MappedDataToDB(Data, folderpath.Replace(folderName, "") + "/" + fileName + ".xml", fileName);
                    getError = UploadHelpers.GetErrorMessage();

                    if (getError == "ERROR")
                    {
                        litWarningError.Text = "Database is not Updated. Please make sure the Data is Valid.";
                        Page.ClientScript.RegisterStartupScript(GetType(), "loadAlertWarning", "AlertWarningBottom()", true);
                    }
                    else
                    {
                        fuBulkUpload.Dispose();
                        Page.ClientScript.RegisterStartupScript(GetType(), "loadAlertSuccess", "AlertSuccessBottom()", true);
                    }

                    File.Delete(path);
                }
            }
            catch (FormatException fx)
            {
                litAlertError.Text = fx.Message;
                Page.ClientScript.RegisterStartupScript(GetType(), "loadAlertFailed", "AlertFailedBottom()", true);
            }
            catch (Exception ex)
            {
                litAlertError.Text = " " + ex.Message;
                Page.ClientScript.RegisterStartupScript(GetType(), "loadAlertFailed", "AlertFailedBottom()", true);
            }
            finally
            {
                if (!string.IsNullOrEmpty(path))
                {
                    File.Delete(path);
                }
                dir.Delete();
            }
            #endregion
        }