javascript 选择文件后如何自动上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12991824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:34:36  来源:igfitidea点击:

How to automatically upload file after file has been chosen

c#javascriptasp.netasp.net-mvc

提问by anthonypliu

I have the following code for uploading a file in my site:

我有以下代码用于在我的网站中上传文件:

@using (Html.BeginForm("UploadProfileImage", "Member", FormMethod.Post, new { @encType = "multipart/form-data" }))
   {

     @Microsoft.Web.Helpers.FileUpload.GetHtml(initialNumberOfFiles: 1, includeFormTag: false, uploadText: "Upload File",allowMoreFilesToBeAdded:false)
    <span class="success">@ViewData["SuccessMessage"]</span>
     <input class="button" type="submit" name="submit" value="Upload" />         

}

I want this to be able to automatically post after the user selects the file from the "browse" button. Currently, the user has to click upload every time the user chooses a file to upload, anyway to make this process automatic?

我希望这能够在用户从“浏览”按钮中选择文件后自动发布。目前,用户每次选择要上传的文件时都必须单击上传,无论如何要使此过程自动?

回答by codingbiz

The file upload control supports onchangeevent. Hope that can be used to trigger the upload

文件上传控件支持onchange事件。希望可以用来触发上传

<form name="upload" action="uploadfile.aspx" method="POST">
    <input name="myfile" type="file" onchange="UploadFile()" />
</form>

<script>
   function UploadFile()
   {
      //do validation here
      document.forms['upload'].submit();
   }
</script>

回答by fubo

ASP:

ASP:

<input type="file" onchange="this.form.submit();" name="fUpload"/>

CodeBehind:

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        System.Web.HttpPostedFile file = Request.Files["fUpload"];
        if (file != null && file.ContentLength > 0)
        {
            file.SaveAs(@"C:\dir\"+System.IO.Path.GetFileName(file.FileName));
        }
    }

}

asp:FileUpload instead of input:

asp:FileUpload 而不是输入:

ASP:

ASP:

<asp:FileUpload runat="server" onchange="this.form.submit();" ID="fuFile"/>

Codebehind:

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        if (fuFile.PostedFile.FileName != string.Empty  && fuFile.PostedFile.ContentLength > 0)
        {
            fuFile.PostedFile.SaveAs(@"C:\dir\" + fuFile.FileName);
        }
    }

}

回答by Pushpendra

I have not use Microsoft.web library but if you can call the java script function Automatically after the file uploded then you can also acheive that.

我没有使用 Microsoft.web 库,但是如果您可以在文件上传后自动调用 java 脚本函数,那么您也可以实现。

If you can call the javascript founction in this line :

如果您可以在此行中调用 javascript 函数:

@Microsoft.Web.Helpers.FileUpload.GetHtml(initialNumberOfFiles: 1, includeFormTag: false, uploadText: "Upload File",allowMoreFilesToBeAdded:false)

回答by Pilgerstorfer Franz

FileUploaddoes not supportany kind of automatically upload a chosen file to the server. You will have to provide some kind of mechanism - see MSDN

FileUpload不支持任何类型的自动将所选文件上传到服务器。您将不得不提供某种机制 - 请参阅MSDN

Saving Uploaded Files

The FileUpload control does not automatically save a fileto the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the specified file.For example, you can provide a button that the user clicks to upload the file. The code that you write to save the specified file should call the SaveAs method, which saves the contents of a file to a specified path on the server. Typically, the SaveAs method is called in an event-handling method for an event that raises a post back to the server. ...

保存上传的文件

FileUpload 控件不会在用户选择要上传的文件后自动将文件保存到服务器。您必须明确提供控制或机制以允许用户提交指定的文件。例如,您可以提供用户单击以上传文件的按钮。您为保存指定文件而编写的代码应调用 SaveAs 方法,该方法将文件内容保存到服务器上的指定路径。通常,在事件处理方法中调用 SaveAs 方法,用于引发回发到服务器的事件。...