WebService部分代码
public string UploadFile(byte[] fs, string FileName)
{
try
{
///定义并实例化一个内存流,以存放提交上来的字节数组。
MemoryStream m = new MemoryStream(fs);
///定义实际文件对象,保存上载的文件。
//FileStream f = new FileStream(Server.MapPath(".") + "\\image\\"
FileStream f = new FileStream(@"E:\01\SmartDeviceProject1\WebService1\" + "\\image\\"
+ FileName, FileMode.Create);
///把内内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
return "文件已经上传成功。";
}
catch (Exception ex)
{
return ex.Message;
}
}
手机端图片文件打开上传代码
{
openFileDialog1.InitialDirectory = GetStorageFolder();
openFileDialog1.Filter = "jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif|bmp files(*.bmp)|*.bmp|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Image img = new Bitmap(openFileDialog1.FileName);
pictureBox1.Image = img;
}
}
private void button2_Click(object sender, EventArgs e)
{
//上传图片
SmartDeviceProject1.localhost_uploadimage.Service1 uf = new Service1();
FileInfo fi = new FileInfo(openFileDialog1.FileName);
byte[] b = new byte[fi.Length];
System.IO.FileStream fs = fi.OpenRead();
fs.Read(b, 0, Convert.ToInt32(fi.Length));
uf.UploadFile(b, "abc" + System.Guid.NewGuid() + ".jpg");
}