利用webservice上传文件
客户端:
页面:加入一个FileUpload 控件,一个Button(btnUpload)
下面是btnUpload的事件:
protected void btnUpload_Click(object sender, EventArgs e)
{
////获得上传文件的名称
//FileInfo file = new FileInfo(fileUpload.PostedFile.FileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//将上传的文件转换为二进制流
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");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
新建一个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;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
页面:加入一个FileUpload 控件,一个Button(btnUpload)
下面是btnUpload的事件:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
新建一个webService 项目,以下是上传方法的代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)