简体   繁体   English

使用ASP.NET和C#将文件上传到服务器

[英]Upload a file to a server using ASP.NET and C#

I am new to forms and am trying to get an understanding of what is going on. 我是表单的新手,正在尝试了解正在发生的事情。 I have looked at lots of questions and tutorials, but feel unclear on certain points. 我看过很多问题和教程,但在某些方面不清楚。

So far I have created the following form in an aspx page: 到目前为止,我已经在aspx页面中创建了以下表单:

<form id="uploadbanner" enctype="multipart/form-data" method="post" action="#">
    <span class="txtSmallGrey fl" style="display:block; width:200px; margin:15px; margin-bottom:2px">
        <%= oUtils.GetContentText("Collect_Config_upload_sound") %>
    </span>
    <input type="file"  name="SoundFile" id="SoundFile" style="margin:15px; margin-bottom:2px">        
    <input type="submit" value="Upload" id="submit" style="float:left; margin-left:245px; margin-top:1px; height:20px;">    
</form>

and I have the following script at the top of the page: 并且在页面顶部有以下脚本:

<%
        if(Request.Form["SoundFile"] != "")
        {
            HttpPostedFile file = Request.Files["SoundFile"];
            string fname = Path.GetFileName(file.FileName);
            file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
        }
%>

I have a reasonable understanding of AJAX so some of this seems familiar to me. 我对AJAX有一个合理的了解,因此其中一些对我来说似乎很熟悉。

Now to explain what I understand: 现在解释一下我的理解:

The form is declared and given the id of 'uploadbanner'. 声明该表单,并为其指定ID“ uploadbanner”。 As I am transferring a file I have to include 'enctype...' I am posting as it is more secure and more flexible. 在传输文件时,我必须添加“ enctype ...”,因为它更安全,更灵活。

The action term tells the form where to post to. 操作项告诉表单要发布到的位置。 In this case I have put the C# code at the top of the page so do not need to include an asp.net page address to process this. 在这种情况下,我将C#代码放在页面的顶部,因此不需要包含asp.net页面地址即可进行处理。 If I did, I would include an asp.net page, in the same way as I would for AJAX (I think?). 如果这样做,我将以与AJAX相同的方式包括一个asp.net页面(我认为吗?)。

Anything with an input tag inside the form tags will be posted in the form, and will send the name and value. 表单标签中带有输入标签的所有内容都将发布在表单中,并发送名称和值。

When the submit button is pressed, the form will be submitted to the the server side code for processing. 当按下提交按钮时,表单将被提交到服务器端代码进行处理。 So far I feel I understand what is going on. 到目前为止,我觉得我了解发生了什么事。

Now the parts I feel less clear about, 现在我不太清楚的部分

  • Is it the case the when the 'submit' button is pressed, the C# code at the top of the page will be activated, and so if the field was blank it would not do anything? 如果按下“提交”按钮时,页面顶部的C#代码将被激活,那么如果该字段为空白,它将什么都不做?
  • And if the button was pressed multiple times, would the form be submitted multiple times? 如果多次按下该按钮,是否可以多次提交表单?
  • If so, then this is much the same way as AJAX?, and the file will simply be passed to the C# code, from where I can do what I need with it? 如果是这样,那么这与AJAX?几乎相同,并且该文件将简单地传递到C#代码,从那里我可以使用它来做我需要的工作?
  • My final questions is, can I submit the form using an alternate method to the submit button, eg can I make a normal JavaScript button and tell it to submit the form? 我的最后一个问题是,我可以使用其他方法向提交按钮提交表单吗,例如,我可以制作一个普通的JavaScript按钮并告诉其提交表单吗?

Is it the case the when the 'submit' button is pressed, the C# code at the top of the page will be activated, and so if the field was blank it would not do anything? 如果按下“提交”按钮时,页面顶部的C#代码将被激活,那么如果该字段为空白,它将什么都不做?

Yes, Every time your page loads it will run the C# code, One would assume that if you submit the form it will check the posted form data. 是的,您的页面每次加载都会运行C#代码,人们会假设如果您提交表单,它将检查过帐的表单数据。 It's probably best to check HTTP headers. 最好检查HTTP标头。

And if the button was pressed multiple times, would the form be submitted multiple times? 如果多次按下该按钮,是否可以多次提交表单?

Yes, the form will be submitted multiple times. 是的,该表格将被多次提交。

If so, then this is much the same way as AJAX?, and the file will simply be passed to the C# code, from where I can do what I need with it? 如果是这样,那么这与AJAX?几乎相同,并且该文件将简单地传递到C#代码,从那里我可以使用它来做我需要的工作?

It's similar in the sense of HTTP requests but your posting to the page with a file attached and then C# checks with the page has file attached by running the Request.Form and then Request.Files and Posts to the server. 从HTTP请求的角度来看,这是相似的,但是您先在页面上附加文件,然后C#通过运行Request.Form,然后再将Request.Files和Posts发送到服务器,检查页面是否附加了文件。

My final questions is, can I submit the form using an alternate method to the submit button, eg can I make a normal JavaScript button and tell it to submit the form? 我的最后一个问题是,我可以使用其他方法向提交按钮提交表单吗,例如,我可以制作一个普通的JavaScript按钮并告诉其提交表单吗?

What do you mean by a normal JavaScript button? 普通的JavaScript按钮是什么意思? You don't have to use a submit button. 您不必使用提交按钮。 As long as your passing in a file and the page is loaded, the code will still run. 只要您传入文件并加载页面,代码仍将运行。 I have approach where as i post to a HTTPHandler. 当我发布到HTTPHandler时,我有办法。 Code snippet below: 下面的代码段:

public void ProcessRequest(HttpContext context)
 {
    context.Response.ContentType = "text/plain";
    HttpPostedFile postedFile = context.Request.Files["Filedata"];
    string filename = postedFile.FileName;
    var Extension = filename.Substring(filename.LastIndexOf('.') 
    + 1).ToLower();

    string savepath =HttpContext.Current.Server.MapPath("/images/profile/");

    postedFile.SaveAs(savepath + @"\" + user.uid + filename);
 }

So when submitting i point to the HttpHandler.ashx which is a class that implements the IHttpHandler interface, Which in turn gets the current context. 因此,在提交时,我指向HttpHandler.ashx,这是一个实现IHttpHandler接口的类,而该接口又获取当前上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM