开发者

How to upload files on server?

I have deployed my software on a server,In my softw开发者_StackOverfloware there is a tool which reads an excel file and displays the content in a gridView.

It's working fine on my stand-alone PC, how can I do it on the web?

Should I upload my excel files on server and then read it or directly read it from the user's PC?

Any solutions?


you need to put fileupload control on your page

<asp:FileUpload ID="fup" runat="server" />

and then in code behind, save this file

 fup.SaveAs(Server.MapPath("~/upload/temp/" + fup.FileName ));

you can take a look at this article

Performing a File Upload using ASP.NET 2.0 and C# .NET

and Simple File Upload Control with ASP.NET and C#


I am not getting your question so far.Which error occurs on web?

Here is the sample code which may help you. Every time it uploads excel file on server from client machine and get data from that and bind grid.

      protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {    
            if (AsyncFileUpload1.HasFile)
            {
                string strPath = MapPath("~/Uploads/") + System.IO.Path.GetFileName(e.filename);
                 // gets extension of a file to check for a valid excel file
                string ext = System.IO.Path.GetExtension(e.filename); 
                if (ext.ToLower() == ".xlsx" || ext.ToLower() == ".xls")
                {
                    AsyncFileUpload1.SaveAs(strPath);
                }       
                DataTable dt = getdata(strPath); // get data from excel file
                BindGrid(dt); 
            }


        }


You can't read it directly from users PC, you need to upload files anyway.


It's working fine on your local environment because effectively your website and the file you're trying to read are on the same machine. As a result, the web server can just load the file directly and there's no problem.

In a live environment, the website will be on a server somewhere while the excel file will be on the user's on own pc. In this scenario the web server can't access the file and you will have to upload to the web server by means of a file upload dialog. You can then save this file to a safe folder you specify (ideally in your web.config in case you change your mind or need to move it) somewhere on your server and access the file to display, manipulate or even insert into a database should you choose to.


I don't think you should save it to your server unless you really have to (which would require Write permissions on the directory you save to). If you are using Open XML to read the Excel file you can read the posted stream directly without saving to file first via SpreadsheetDocument.Open(FileUpload1.PostedFile.InputStream, false)...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜