在我的ASP.net应用程序中上传Excel时出现“线程被中止”错误

时间:2013-04-11 11:48:15

标签: c# asp.net multithreading

我正在我的应用程序中上传一个excel文件并从中读取数据。这是我的代码..

代码

protected void btnBulkNext_Click(object sender, EventArgs e)
{
    try
    {
        string filePath = uplBulkUpload.FileName;

        string fileExtension = Path.GetExtension(filePath);
        uplBulkUpload.SaveAs(Server.MapPath(filePath));
        List<UserDataInExcel> uploadedData = new List<UserDataInExcel>();

        if (fileExtension == Constant.FORMAT_XLS || fileExtension == Constant.FORMAT_XLSX)
        {
            uploadedData = _grantAccessHandler.ReadUserDataFromExcel(Server.MapPath(filePath), fileExtension);
        }
        else
         {
        }
        if (uploadedData == null)
        {
            lblError.Visible = true;
            lblError.Text = Constant.ERROR_BULK_TEMPLATE_NOT_MATCHING;
        }
        else if (uploadedData.Count < 1)
        {
            lblError.Visible = true;
            lblError.Text = Constant.ERROR_EMPTY_EXCEL;
        }
        else
        {
            SessionManager.Session.Current.BulkUploadedData = uploadedData;
            List<ErrorDetail> error = _grantAccessHandler.CreateAccessForUploadedUsers(uploadedData);
            SessionManager.Session.Current.ErrorDetails = error;
            Response.Redirect("~/ResultSummary.aspx?frompage=bulkupload", false);
        }
    }
    catch (Exception ex)
    {
        ErrorHandler errorHandler = new ErrorHandler();
        errorHandler.HandleException(ex, "Bulk Upload-btnBulkNext_Click");
        Response.Redirect("~/ErrorSummaryPage.aspx");
    }
}

public List<UserDataInExcel> ReadUserDataFromExcel(string filePath, string fileExtension)
{
    FileStream excelFileStream = File.Open(filePath, FileMode.Open, FileAccess.Read);

    IExcelDataReader excelDataReader;
    DataSet dsExcelData = new DataSet();
    if (fileExtension == Constant.FORMAT_XLS)
    {
        excelDataReader = ExcelReaderFactory.CreateBinaryReader(excelFileStream);
        excelDataReader.IsFirstRowAsColumnNames = true;
        dsExcelData = excelDataReader.AsDataSet();
    }
    else if (fileExtension == Constant.FORMAT_XLSX)
    {
        excelDataReader = ExcelReaderFactory.CreateOpenXmlReader(excelFileStream);
        excelDataReader.IsFirstRowAsColumnNames = true;
        dsExcelData = excelDataReader.AsDataSet();
    }
    List<UserDataInExcel> excelDataCollection = new List<UserDataInExcel>();
    List<ErrorDetail> error = new List<ErrorDetail>();
    ErrorDetail errorDetail;
    UserDataInExcel userDataInExcel;
    int count = 0;
    SessionManager.Session.Current.bulkTotalServer = dsExcelData.Tables[0].Rows.Count;
    var regexItem = new Regex("^[a-zA-Z0-9_ ]*$");
    if (dsExcelData.Tables.Count > 0 && dsExcelData.Tables[0].Rows.Count > 0)
    {
        int ReturnCode = validateExcelData(dsExcelData);
        if (ReturnCode == 0)
        {
            foreach (DataRow dRow in dsExcelData.Tables[0].Rows)
            {
                if (dRow.ItemArray[0].ToString() != null && dRow.ItemArray[0].ToString() != string.Empty)
                {
                    count++;
                    try
                    {
                        userDataInExcel = new UserDataInExcel();
                        if (String.IsNullOrEmpty(dRow.ItemArray[0].ToString()) || !regexItem.IsMatch(dRow.ItemArray[0].ToString()))
                        {
                            errorDetail = new ErrorDetail();
                            errorDetail.ErrorDescription = Constant.ERROR_BULK_INVALID_ROW + count + " -" + Constant.ERROR_BULK_INVALID_INPUT;
                            errorDetail.UserId = dRow.ItemArray[0].ToString();
                            error.Add(errorDetail);
                            continue;
                        }
                        else
                            userDataInExcel.UserId = dRow.ItemArray[0].ToString();

                        if (String.IsNullOrEmpty(dRow.ItemArray[1].ToString()) ||
                            String.IsNullOrEmpty(dRow.ItemArray[2].ToString()) ||
                            !regexItem.IsMatch(dRow.ItemArray[1].ToString()) ||
                            !regexItem.IsMatch(dRow.ItemArray[2].ToString()))
                        {
                            errorDetail = new ErrorDetail();
                            errorDetail.ErrorDescription = Constant.ERROR_BULK_INVALID_ROW + count + " -" + Constant.ERROR_BULK_INVALID_INPUT;
                            errorDetail.UserId = dRow.ItemArray[0].ToString();
                            error.Add(errorDetail);
                            continue;
                        }
                        else
                            userDataInExcel.UserName = dRow.ItemArray[2].ToString() + "," + dRow.ItemArray[1].ToString();

                        if (String.IsNullOrEmpty(dRow.ItemArray[3].ToString()) || !regexItem.IsMatch(dRow.ItemArray[3].ToString()))
                        {
                            errorDetail = new ErrorDetail();
                            errorDetail.ErrorDescription = Constant.ERROR_BULK_INVALID_ROW + count + " -" + Constant.ERROR_BULK_INVALID_INPUT;
                            errorDetail.UserId = dRow.ItemArray[0].ToString();
                            error.Add(errorDetail);
                            continue;
                        }
                        else
                            userDataInExcel.MirrorUserId = dRow.ItemArray[3].ToString();

                        if (String.IsNullOrEmpty(dRow.ItemArray[4].ToString()) || !regexItem.IsMatch(dRow.ItemArray[4].ToString()))
                        {
                            errorDetail = new ErrorDetail();
                            errorDetail.ErrorDescription = Constant.ERROR_BULK_INVALID_ROW + count + " -" + Constant.ERROR_BULK_INVALID_INPUT;
                            errorDetail.UserId = dRow.ItemArray[0].ToString();
                            errorDetail.MirrorId = dRow.ItemArray[3].ToString();
                            errorDetail.ErrorOnServer = dRow.ItemArray[4].ToString();
                            error.Add(errorDetail);
                            continue;
                        }
                        else
                            userDataInExcel.Server = dRow.ItemArray[4].ToString();

                        excelDataCollection.Add(userDataInExcel);
                        SessionManager.Session.Current.ErrorDetails = error;
                    }
                    catch (Exception ex)
                    {
                        excelDataCollection = null;
                    }
                }
            }
        }
        else
        {
            excelDataCollection = null;
        }
    }

    if (System.IO.File.Exists(filePath))
    {
        System.IO.File.Delete(filePath);
    }
    return excelDataCollection;
}

应用程序在我的本地解决方案中正常工作,但是当我在IIS中发布代码时,应用程序正在抛出异常

The thread was being aborted. 

为什么会发生此异常以及如何解决此问题。

0 个答案:

没有答案