我有一个简单的 ASP.NET 页面,用户可以浏览到该页面,从他们的机器中选择一个文件并将其上传到我的服务器。我的远程用户希望在他们结束时自动上传文件(使用 CURL)。
由于没有用户单击按钮,我需要做一些事情来处理这个问题,但是什么?
我需要进行哪些更改才能让我的网页在不单击按钮的情况下处理文件上传?
代码页...
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace FileUpload
{
public partial class _Default : System.Web.UI.Page
{
string UploadTo_Path = ConfigurationManager.AppSettings.Get("UploadTo_Path");
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected void Page_Load(object sender, EventArgs e){ }
protected void Button1_Click(object sender, EventArgs e)
{
UploadFile();
}
private void UploadFile()
{
if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
UploadTo_Path += "\\" + fn;
File1.PostedFile.SaveAs(UploadTo_Path);
Response.Write("The file has been uploaded.");
}
else
{
Response.Write("Please select a file to upload.");
}
}
}
}
前端...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FileUpload._Default" Trace="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>File Upload</title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="file" id="File1" name="File1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>