加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

文件上传的方法

发布时间:2020-12-17 02:50:23 所属栏目:安全 来源:网络整理
导读:?一。利用webservice上传文件 客户端: 页面:加入一个FileUpload 控件,一个Button(btnUpload) 下面是btnUpload的事件: ? protected ? void ?btnUpload_Click( object ?sender,?EventArgs?e) ???? {???? ???????? /**/ /// /获得上传文件的名称 ???????? /

?一。利用webservice上传文件

客户端:
页面:加入一个FileUpload 控件,一个Button(btnUpload)
下面是btnUpload的事件:

? protected ? void ?btnUpload_Click( object ?sender,?EventArgs?e)

????
{????

????????
////获得上传文件的名称

????????//FileInfo?file?=?new?FileInfo(fileUpload.PostedFile.FileName);


????????
//将上传的文件转换为二进制流

????????byte[]?fileContent?=?fileUpload.FileBytes;

????????
//获得上传文件的名称

????????string?fileName?=?fileUpload.FileName;

????????
//实例化webservice

????????AdService.Service?adTemp?=?new?AdService.Service();

????????
if?(adTemp.UploadFile(fileContent,?fileName)) //调用上传方法。

????????{

????????????Response.Write(
"OK");

????????}

????????
else

????????
{

????????????Response.Write(
"error");

????????}


????}


新建一个webService 项目,以下是上传方法的代码:

///?<summary>

????
///?通过WebService上传文件

????
///?</summary>

????
///?<param?name="fs">文件二进制流</param>

????
///?<param?name="fileName">文件名</param>

????
///?<returns></returns>

????[WebMethod(Description? = ? " web提供的方法,上传文件到相应的地址 " )]????

????
public ? bool ?UploadFile( byte []?fs,? string ?fileName)

????
{

????????
try

????????
{

????????????
///定义并实例化一个内存流,以存放提交上来的字节数组。

????????????System.IO.MemoryStream?m?=?new?System.IO.MemoryStream(fs);

????????????
//取出存放地址,可以通过数据库里存放,不用定死了。此处只是做DEMO。

????????????string?strFile?=?"E:"?+?"//"?+?"Personal?files"?+?"//"?+?"good?things"?+?"//"?+fileName;

????????????
///定义实际文件对象,保存上载的文件。

????????????System.IO.FileStream?fl?=?new?System.IO.FileStream(strFile,?FileMode.OpenOrCreate);

????????????
///把内内存里的数据写入物理文件

????????????m.WriteTo(fl);????????????

????????????m.Close();

????????????fl.Close();

????????????m?
=?null;

????????????fl?
=?null;

????????????
return?true;

????????}

????????
catch

????????
{

????????????
return?false;

????????}


????}

?

二。利用控件上传文件

.aspx页面代码

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
??? <title>无标题页</title>
</head>
<script runat="server">
???
</script>

<body>
??? <form id="form1" runat="server">
??? <div>
??????? <ASP:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<ASP:Button ID="Button1" runat="server" OnClick="Button1_Click"
?Text="Upload File" /> <br />
<br />
<ASP:RegularExpressionValidator
?id="RegularExpressionValidator1" runat="server"
?ErrorMessage="Only mp3,m3u or mpeg files are allowed!"

//下面的文件类型帅选代码,可以在aspx.cs页面中写更为妥当
?ValidationExpression="^(([a-zA-Z]:)|(//{2}/w+)/$?)(//(/w[/w].*))+(.jpg|.JPG|.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$"
?ControlToValidate="FileUpload1"></ASP:RegularExpressionValidator>
<br />
<ASP:RequiredFieldValidator
?id="RequiredFieldValidator1" runat="server"
?ErrorMessage="This is a required field!"
?ControlToValidate="FileUpload1"></ASP:RequiredFieldValidator>


??????? <br />
??????? <ASP:Label ID="Label1" runat="server"></ASP:Label>
??????? <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></div>
??? </form>

</body>
</html>

aspx.cs页面代码

protected void Button1_Click(object sender,EventArgs e)
??? {
??????? if (FileUpload1.HasFile)
??????? {
???????????
??????????? //string clientpath = FileUpload1.PostedFile.FileName.ToString();???????????
??????????? if(FileUpload1.PostedFile.ContentLength!=0)
???????????? {???????????????
??????????????? try
??????????????? {
??????????????????? string pathfirst = "C://Uploads//" + FileUpload1.FileName;
??????????????????? FileUpload1.SaveAs("C://Uploads//" +
???????????????????????? FileUpload1.FileName);
??????????????????? Label1.Text = "File name: " +
???????????????????????? FileUpload1.PostedFile.FileName + "<br>" +
???????????????????????? FileUpload1.PostedFile.ContentLength + " kb<br>" +
???????????????????????? "Content type: " +
???????????????????????? FileUpload1.PostedFile.ContentType;
??????????????????? string sql = "insert into test(path) values('" + pathfirst + "')";
??????????????????? SqlConnection con = new SqlConnection("Data Source=192.168.1.69;Initial Catalog=calendar;Integrated Security=False;user ID=sa;Password=huison");
??????????????????? SqlCommand com = new SqlCommand(sql,con);
??????????????????? con.Open();
??????????????????? com.ExecuteNonQuery();
??????????????????? con.Close();
??????????????????? Label2.Text = FileUpload1.FileName;

??????????????? }
??????????????? catch (Exception ex)
??????????????? {
??????????????????? Label1.Text = "ERROR: " + ex.Message.ToString();
??????????????? }
??????????? }
??????????? else
??????????? {
??????????????? Label1.Text = "This file does not exist.";
??????????? }
??????? }
??????? else
??????? {
??????????? Response.Write("You have not specified a file.");

??????? }??? }

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读