通过Web Service如何上传一个文件啊?

Soft21 2002-11-22 04:37:22
我想用Dot Net 写一个Web Service,让调用者给我上传一个文件。
请问我的Web Service该如何实现?
...全文
92 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wl0802 2002-11-23
  • 打赏
  • 举报
回复
在客户端(winform/asp.net)将文件化为byte[]
调用webservice的upload(byte[] file)方法,
将byte[]再组合成文件
[WebMethod]
public void Upload_file(Files file,byte[] filebyte)//上传文件
{
try
{
file.upload_time=DateTime.Now.ToString();
SelectCommand.CommandText="insert into DOWNLOAD_FILES(type,upload_time,fsize,clicktimes,discription,ownerid,property,filename) values('"+file.type+"','"+file.upload_time+"',"+file.fsize+","+file.clicktimes+",'"+file.discription+"',"+file.ownerID+",'"+file.property+"','"+file.filename+"')";
SelectCommand.Connection=Connect;
Connect.Open();
SelectCommand.ExecuteNonQuery();

SelectCommand.CommandText="select MAX(id) from DOWNLOAD_FILES";
SelectCommand.Connection=Connect;
int id=(int)(SelectCommand.ExecuteScalar());
FileStream s=new FileStream("C:\\Inetpub\\wwwroot\\files\\"+id,FileMode.Create);
s.Write(filebyte,0,filebyte.Length);
s.Close();
Connect.Close();
}
catch(Exception Upload_file_ex)
{
throw Upload_file_ex;
}
}



ASP.Net调用webservice:
private void Button1_Click(object sender, System.EventArgs e)
{
if(uplTheFile.PostedFile.ContentLength==0)Response.Write("请选择文件!");
else
{
Stream sfile=uplTheFile.PostedFile.InputStream;
byte[] filebyte=new byte[uplTheFile.PostedFile.ContentLength];
sfile.Read(filebyte,0,uplTheFile.PostedFile.ContentLength);
MICService.Files ff=new MICService.Files();
ff.fsize=uplTheFile.PostedFile.ContentLength;
ff.clicktimes=0;
ff.ownerID=Convert.ToInt32(Session["id"]);
ff.discription=dis.Text;
ff.type=type.SelectedItem.Value;
ff.property =property.SelectedItem.Value;
string [] split=(uplTheFile.PostedFile.FileName.ToString()).Split(new char[]{'\\'}) ;
ff.filename=split[split.Length-1];
ss.Upload_file(ff,filebyte);
Response.Redirect("netdisk.aspx");
}
}

给分!给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分给分

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧