文件上传代码 [英] code for file upload

查看:63
本文介绍了文件上传代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我有关信息,

上传按钮后面的代码以C#语言在ASP.NET中上传文件

谢谢::困惑::困惑::困惑:

Please tell me About,

code behind upload button to upload a file in ASP.NET in C# language

Thanks:confused::confused::confused:

推荐答案

上传按钮实际上是一个输入标记,这是用HTML上载文件的唯一方法.服务器上的类很漂亮,可以自我记录,文件字节和文件名都可以在其中访问,并可以使用所需的任何方式. Web上的MSDN示例和示例将向您展示有关使用此非常简单的控件的所有信息.如果要使用ASP.NET,则应该至少拥有一本不错的ASP.NET书籍,其中还包含示例.
The upload button is in fact an input tag, that''s the only way to upload a file in HTML. The class, on the server, is pretty self documenting, the file bytes and file name are there for you to access, and use any way you want. The MSDN samples and examples on the web will show you everything there is to know about using this very simple control. If you want to use ASP.NET, you should own at least one good ASP.NET book, which would also contain samples.


查看一个简单的示例
See an easy example Here[^]

Besides, Google is always your best friend :)


在aspx页面中使用以下html输入控件
Use following html input control in your aspx page
<input id="filMyFile" type="file" runat="server"></input>



在.cs文件中编写以下代码



Write following code in .cs file

protected HtmlInputFile filMyFile;

if( filMyFile.PostedFile != null )
{
    // File was sent

}
else
{
    // No file

}



要读取上传的文件,请使用以下代码



To read the uploaded file use following code

byte[] myData = new byte[nFileLen];

myFile.InputStream.Read(myData, 0, nFileLen);



将上传的文件保存到磁盘



Saving uploaded file to disk

private void WriteToFile(string strPath, ref byte[] Buffer)
{
    // Create a file

    FileStream newFile = new FileStream(strPath, FileMode.Create);

     // Write data to the file

    newFile.Write(Buffer, 0, Buffer.Length);

    // Close file

    newFile.Close();
}



希望这对您有所帮助:)



Hope this may help u :)


这篇关于文件上传代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆