【发布时间】:2014-08-06 13:08:08
【问题描述】:
编辑:作业发生了巨大变化 - 抱歉。
现在我必须编写一个 Web 表单,该表单将采用 CSV 文件,然后在网格中显示其内容。仍在 C# 和 ASP.NET 中,但没有 SQL。
C#
//This is the webform
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace PatientImport
{
public partial class WebForm1 : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("Data") + "\\" + fn;
try
{
if (fn.Substring(fn.Length - 3) == "csv" || fn.Substring(fn.Length - 3) == "CSV")
{
File1.PostedFile.SaveAs(SaveLocation);
Response.Write("The file has been uploaded.");
}
else
{
Response.Write("Incorrect type. Choose a .csv file to upload.");
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
//Note: Exception.Message returns a detailed message that describes the current exception.
//For security reasons, we do not recommend that you return Exception.Message to end users in
//production environments. It would be better to put a generic error message.
}
}
else
{
Response.Write("Choose a .csv file to upload.");
}
}
}
}
HTML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="PatientImport.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" method="post" enctype="multipart/form-data" runat="server">
<INPUT type=file id=File1 name=File1 runat="server" />
<br>
<input type="submit" id="Submit1" value="Upload" runat="server" />
<div>
</div>
</form>
</body>
</html>
【问题讨论】:
-
我只想让您的应用程序调用一个 Web 方法,该方法在文件上传后将数据加载到 SQL 中。
-
您有什么问题?上传文件?解析文件?
-
如果没有看到您的原始代码,我们将不知道如何对您现有的代码库进行更改。请发帖a minimal example of what needs to change,并详细说明需要修改的地方。
-
正如@gunr2171 所说,您应该发布a minimal example of what needs to change。如果没有看到一些代码,我们就无法帮助您,因为我们不知道您要做什么。
-
请再看一遍,我加了代码,赋值变了。
标签: c# asp.net csv datagrid webforms