简体   繁体   English

如何在不使用ASP.NET服务器端控件的情况下从C#ASP.NET中读取HTML文件类型的输入流

[英]How to read inputstream from HTML file type in C# ASP.NET without using ASP.NET server side control

I have the following form 我有以下表格

<form id="upload" method="post" EncType="Multipart/Form-Data" action="reciver.aspx">
        <input type="file" id="upload" name="upload" /><br/>
        <input type="submit" id="save" class="button" value="Save" />            
</form>

When I look at the file collection it is empty. 当我查看文件集时,它是空的。

HttpFileCollection Files = HttpContext.Current.Request.Files;

How do I read the uploaded file content without using ASP.NET server side control? 如何在不使用ASP.NET服务器端控件的情况下读取上载的文件内容?

Why do you need to get the currect httpcontext, just use the page's one, look at this example: 为什么你需要获取当前的httpcontext,只需使用页面的一个,看看这个例子:

//aspx
<form id="form1" runat="server" enctype="multipart/form-data">
 <input type="file" id="myFile" name="myFile" />
 <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>

//c#
protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile file = Request.Files["myFile"];
    if (file != null && file.ContentLength )
    {
        string fname = Path.GetFileName(file.FileName);
        file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname)));
    }
}

The example code is from Uploading Files in ASP.net without using the FileUpload server control 示例代码来自ASP.net中的Uploading Files,而不使用FileUpload服务器控件

Btw, you don't need to use a server side button control. 顺便说一句,您不需要使用服务器端按钮控件。 You can add the above code to the page load where you check if the current state is a postback. 您可以将上面的代码添加到页面加载,您可以在其中检查当前状态是否为回发。

Good luck! 祝好运!

Here is my final solution. 这是我的最终解决方案。 Attaching the file to an email. 将文件附加到电子邮件。

//Get the files submitted form object
            HttpFileCollection Files = HttpContext.Current.Request.Files;

            //Get the first file. There could be multiple if muti upload is supported
            string fileName = Files[0].FileName;

            //Some validation
            if(Files.Count == 1 && Files[0].ContentLength > 1 && !string.IsNullOrEmpty(fileName))
            { 
                //Get the input stream and file name and create the email attachment
                Attachment myAttachment = new Attachment(Files[0].InputStream, fileName);

                //Send email
                MailMessage msg = new MailMessage(new MailAddress("emailaddress@emailaddress.com", "name"), new MailAddress("emailaddress@emailaddress.com", "name"));
                msg.Subject = "Test";
                msg.Body = "Test";
                msg.IsBodyHtml = true;
                msg.Attachments.Add(myAttachment);

                SmtpClient client = new SmtpClient("smtp");
                client.Send(msg);
            }

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

相关问题 如何使用ASP.NET和C#从服务器端确定浏览器类型? - How to determine Browser type from Server side using ASP.NET & C#? 如何使用C#从zip文件夹中读取文件而不在asp.net 2中解压缩 - How to read file from zip folder without extracting in asp.net 2 using C# 我如何使用反射从文件后面的asp.net代码访问服务器端控件? - How can i access a server side control from asp.net code behind file using reflection? 从服务器端Asp.net和c#生成书签 - Generate Bookmark This from server side Asp.net and c# 使用上载文件时 <input> C#asp.net服务器端的元素,C#如何访问文件属性(如文件大小或文件类型)? - When uploading a file using <input> element to a C# asp.net server side, how can the C# access file attributes like file size or file type? 如何使用C#从文件中读取内容并在ASP.NET页面上将其与HTML代码一起使用? - How to read something from a file with C# and use it with HTML codes on ASP.NET page? 如何在服务器端为C#/ ASP.NET中的图像控件添加单击事件 - How to add an on click event in server side for an Image control in C#/ASP.NET C#ASP.NET用户控件如何处理div点击服务器端? - C# ASP.NET User control How to handle a div click server side? 从asp.net读取html文件 - read html file from asp.net 如何从asp.net端调用C#方法? - How to call a C# method from asp.net side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM