我有一個簡單的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>
檢查System.Web.HttpContext.Current.Request.HttpMethod的值將執行此操作。謝謝勞埃德,非常感謝。 – cymorg
請注意,在WebForm頁面上,您可以直接檢查Request.HttpMethod,我認爲,不需要漫長的上下文位。 – Lloyd