我在我的asp.net网页上有一个方法将csv文件转储到我的gridview中但是我想要包含一个对话框供用户浏览并从他们的PC中选择一个csv文件来导入并获取该文件名和路径信息提供给我的csv导入方法,以便它可以作用于该文件。有一种简单的方法可以做到这一点吗?
答案 0 :(得分:0)
您需要此类System.Web.UI.HtmlControls.HtmlInputFile
来自MSDN:
使用HtmlInputFile服务器控件来处理上传二进制文件或 从浏览器客户端到服务器的文本文件。文件上传适用于 Microsoft Internet Explorer 3.02或更高版本。
更新:这是来自MSDN(.net v1.1)
的工作代码示例<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<script language="VB" runat="server">
Sub Button1_Click(Source As Object, e As EventArgs)
If Text1.Value = "" Then
Span1.InnerHtml = "Error: you must enter a file name"
Return
End If
If Not (File1.PostedFile Is Nothing) Then
Try
File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _
Text1.Value & "</b> on the Web server"
Catch exc As Exception
Span1.InnerHtml = "Error saving file <b>c:\temp\" & _
Text1.Value & "</b><br>" & exc.ToString()
End Try
End If
End Sub 'Button1_Click
</script>
</head>
<body>
<h3>HtmlInputFile Sample</h3>
<form enctype="multipart/form-data" runat="server">
Select File to Upload:
<input id="File1"
type="file"
runat="server">
<p>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server">
<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />
<p>
<input type=button
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">
</form>
</body>
</html>
答案 1 :(得分:0)
到目前为止,每个人的回答似乎都是正确的。另一个选择是使用ASP.NET FileUpload control,如果你想坚持服务器端控件。
这是一个如何使用我无耻地从here偷走的控件的示例:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
String savePath = @"c:\temp\uploads\";
// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = FileUpload1.FileName;
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the
// uploaded file to the specified path.
// This example does not perform all
// the necessary error checking.
// If a file with the same name
// already exists in the specified path,
// the uploaded file overwrites it.
FileUpload1.SaveAs(savePath);
// Notify the user of the name of the file
// was saved under.
UploadStatusLabel.Text = "Your file was saved as " + fileName;
}
else
{
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</div>
</form>
</body>
</html>
答案 2 :(得分:0)
对于 ASP.NET(即 Web 应用程序而不是桌面),将文件上传到 Web 服务器(在这种情况下可能不需要,但原始问题在这一点上不清楚),我建议从 VS 工具箱的 asp:fileupload 控件开始。