FileUpload1.PostedFile.FileName引发异常 [英] FileUpload1.PostedFile.FileName is throwing exception

查看:197
本文介绍了FileUpload1.PostedFile.FileName引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET读取excel文件中的数据.我正在使用文件上传控件来读取文件.我能够从本地计算机中的文件中读取数据,但是在将代码部署到服务器中之后,当我尝试从客户端计算机中读取文件时,却出现了异常.

I am using ASP.NET to read the data in the excel file. I am using a file upload control to read the file. I am able to read the data from the file in my local machine, but after deploying my code in the server, when I try to read the file from the client machine, I am getting an exception.

FileUpload1.PostedFile.FileName正在服务器中引发异常.

FileUpload1.PostedFile.FileName is throwing the exception in the server.

异常消息是:

'D:\path in client machine\MyExcel.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct. If you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.

请帮助.

代码:

<add key="OleDbConnection" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
          FilePath ;Extended Properties=&quot;Excel 8.0;HDR=Yes;IMEX=1&quot;"/>

string OleDbConnection = 
    ConfigurationManager.AppSettings["OleDbConnection"].ToString().Replace("FilePath", 
        fileUpload.PostedFile.FileName).Trim();

Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
Excel.Workbooks xlWorkBooks = (Excel.Workbooks)xlApp.Workbooks;

Excel.Workbook wb = xlWorkBooks._Open(fileUpload.PostedFile.FileName, Type.Missing,
    false, Type.Missing, "", "", true, Excel.XlPlatform.xlWindows, "\t", true,
    false, Type.Missing, true);

    string strSheetName = ((Excel.Worksheet)wb.Sheets[1]).Name.ToString();
    xlWorkBooks.Close();
    xlApp.Quit();

   oledbCommand = new OleDbCommand();
   oledbAdapter = new OleDbDataAdapter();
   DataSet dsExcellData = new DataSet();


   oledbConnection = new OleDbConnection(OleDbConnection); 
   oledbConnection.Open();
   oledbCommand.Connection = oledbConnection;

   oledbCommand.CommandText = "Select * from [" + strSheetName + "$]"; 
   oledbAdapter.SelectCommand = oledbCommand;
   oledbAdapter.Fill(dsExcellData);
   return dsExcellData

我正在发布我拥有的示例代码

Hi I am posting the sample code that i am having

添加一个fie上传控件和一个按钮.从客户端计算机上选择文件后,在该按钮上单击以执行以下代码.

Add a fie upload control and a button.On the button click execute the below code after selecting the file from the client machine.

string OleDbConnection ="Provider = Microsoft.Jet.OLEDB.4.0;数据源= \" FilePath \;扩展属性= \" Excel 8.0; HDR =是; IMEX = 1 \"; OleDbConnection = OleDbConnection.Replace("FilePath",FileUpload1.PostedFile.FileName); Label6.Text = OleDbConnection;

string OleDbConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= \"FilePath\";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""; OleDbConnection = OleDbConnection.Replace("FilePath", FileUpload1.PostedFile.FileName); Label6.Text = OleDbConnection;

        string strSheetName = "ASSET_RECV";
        OleDbConnection oledbConnection;
        OleDbCommand oledbCommand;
        OleDbDataAdapter oledbAdapter;

        oledbCommand = new OleDbCommand();
        oledbAdapter = new OleDbDataAdapter();
        DataSet dsExcellData = new DataSet();

        oledbConnection = new OleDbConnection(OleDbConnection);
        oledbConnection.Open();
        oledbCommand.Connection = oledbConnection;
        //oledbCommand.CommandText = "Select * from [{0}$]";

        oledbCommand.CommandText = "Select * from [" + strSheetName + "$]"; // i want to find this sheet name
        oledbAdapter.SelectCommand = oledbCommand;
        oledbAdapter.Fill(dsExcellData);

        oledbConnection.Close();
        GridView1.DataSource = dsExcellData.Tables[0];
        GridView1.DataBind();

1)在IIS中发布项目.尝试从另一台计算机上运行该应用程序,并从excel文件(从客户端计算机)中读取数据.

1) Publish the project in IIS.Try to run the application from another machine and read the data from the excel file (from client machine).

您将收到以下错误.请帮忙.

you will get the below error .Please help.

Microsoft Jet数据库引擎找不到对象'D:\ FileName.xls'.确保对象存在,并正确拼写其名称和路径名.

The Microsoft Jet database engine could not find the object 'D:\FileName.xls'. Make sure the object exists and that you spell its name and the path name correctly.

推荐答案

我认为您需要先保存文件,然后才能打开它.您可以使用:

I think you need to save the file before you can open it. You can use:

PostedFile.SaveAs()

将其保存到服务器.

有帮助吗?

这篇关于FileUpload1.PostedFile.FileName引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆