服务器可以使用来自客户端的文件,没有它上传到服务器? ASP.NET [英] Can the server use a file from a client without uploading it to the server? ASP.NET

查看:119
本文介绍了服务器可以使用来自客户端的文件,没有它上传到服务器? ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,此刻,客户端将文件上载到服务器所在的服务器,然后使用该文件(.csv文件)来更新SQL数据库上的目录。

这是唯一的方式,服务器可以使用该文件?为它首先被上载到服务器?或者你可以使用该文件没有它上传到服务器的目录?

(使用的Visual Studio 2012,C#和asp.net)

code更新---

ASP上传文件。 (我知道code可以更清洁的书面更好,但我的测试code是从来没有干净)
                    

  // Uplod文件到服务器
                FileUpload1.SaveAs(serverUploadDir + FileUpload1.FileName);                //使用上传的文件更新SQL表
                dbConn.UploadCSVToSql(serverUploadDir + FileUpload1.FileName);

该UploadCSVToSql

 公共无效UploadCSVToSql(字符串文件路径)
        {
            //字符串的文件路径=C:\\\\ ABI员工List.csv的副本;
            StreamReader的SR =新的StreamReader(文件路径);
            串线= sr.ReadLine();
            的String []值= line.Split('');
            DataTable的DT =新的DataTable();
            DataRow的排;
            的foreach(价值串DC)
            {
                dt.Columns.Add(新的DataColumn(直流));
            }            而(!sr.EndOfStream)
            {
                值= sr.ReadLine()分割(,)。
                如果(value.Length == dt.Columns.Count)
                {
                    行= dt.NewRow();
                    row.ItemArray =价值;
                    dt.Rows.Add(行);
                }
            }
            SqlBulkCopy的BC =新SqlBulkCopy的(conn.ConnectionString,SqlBulkCopyOptions.TableLock);
            bc.DestinationTableName =MainDump;
            bc.BatchSize = dt.Rows.Count;
            conn.Open();
            bc.WriteToServer(DT);
            bc.Close();
            conn.Close();


解决方案

我不明白您如何得到从客户机到服务器的文件,而不上传它,但你并不需要把它保存到一个文件夹。如果您使用

 < ASP:文件上传ID =fuMyUpload=服务器/>

控制。你可以得到的数据流,并将其存储在内存中。

 如果(!fuMyUpload.HasFile)
    {
         lblWarning.Text =没有选择的文件;
         返回;
    }    VAR csvData = Encoding.UTF8.GetString(fuCircuitCsv.FileBytes);    使用(VAR读卡器=新StringReader(csvData))
    {
          变种头= reader.ReadLine()分割(,)。
          而((行= reader.ReadLine())!= NULL)
          {
                 变种字段= line.Split(,);
          }
    }

I was wondering, at the moment, the client uploads a file to a directory on the server where the server can then use the file (csv file) to update a SQL database.

Is that the only way the server can use that file? For it to first be uploaded to the server? Or can you use that file without uploading it to a directory of the server?

(using visual studio 2012, C# and asp.net)

Code update---

asp Uploading the file. (I know the code can be cleaner an written better, but my testing code is never clean)

                //Uplod file to the server
                FileUpload1.SaveAs(serverUploadDir + FileUpload1.FileName);

                //Use the Uploaded File to update the sql table
                dbConn.UploadCSVToSql(serverUploadDir + FileUpload1.FileName);

The UploadCSVToSql

  public void UploadCSVToSql(string filepath)
        {
            //string filepath = "C:\\Copy of ABI Employee List.csv";
            StreamReader sr = new StreamReader(filepath);
            string line = sr.ReadLine();
            string[] value = line.Split(',');
            DataTable dt = new DataTable();
            DataRow row;
            foreach (string dc in value)
            {
                dt.Columns.Add(new DataColumn(dc));
            }

            while (!sr.EndOfStream)
            {
                value = sr.ReadLine().Split(',');
                if (value.Length == dt.Columns.Count)
                {
                    row = dt.NewRow();
                    row.ItemArray = value;
                    dt.Rows.Add(row);
                }
            }
            SqlBulkCopy bc = new SqlBulkCopy(conn.ConnectionString, SqlBulkCopyOptions.TableLock);
            bc.DestinationTableName = "MainDump";
            bc.BatchSize = dt.Rows.Count;
            conn.Open();
            bc.WriteToServer(dt);
            bc.Close();
            conn.Close();

解决方案

I don't see how you would get a file from the client to the server without uploading it, but you don't need to store it to a folder. If you use the

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

control. you can get the data in a stream and store it in memory.

    if (!fuMyUpload.HasFile)
    {
         lblWarning.Text = "No file Selected";
         return;
    }

    var csvData = Encoding.UTF8.GetString(fuCircuitCsv.FileBytes);

    using (var reader = new StringReader(csvData))
    {
          var headers = reader.ReadLine().Split(',');
          while ((line = reader.ReadLine()) != null)
          {
                 var fields = line.Split(',');
          }
    }

这篇关于服务器可以使用来自客户端的文件,没有它上传到服务器? ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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