移动质监执法通项目用到如何实现拍照上传功能,写了个简单的Sample.
WEBSERVICE接口:
[WebMethod(Description = "上传照片")]
public bool updatePhoto(string CheckRecordId, byte[][] b, string[] fileName)
{
try
{
//上传照片
if (!(b == null || fileName == null))
{
for (int i = 0; i < b.Length; i++)
{
if (b[i] != null && b[i].Length > 0 && fileName[i] != null && fileName[i] != string.Empty)
{
string filePath = System.Configuration.ConfigurationManager.AppSettings["PhotoFilePath"];
string filetypes = fileName[i].Substring(fileName[i].IndexOf('.') + 1);
filePath += DateTime.Now.ToString("yyyyMMddHHmmss") + "." + filetypes;
// filePath += fileName[i].ToString();
MemoryStream memoryStream = new MemoryStream(b[i]);
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, FileMode.Create);
memoryStream.WriteTo(fileStream);
memoryStream.Close();
fileStream.Close();
memoryStream = null;
fileStream = null;
string url = "http://" + System.Configuration.ConfigurationManager.AppSettings["IP"] + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + filetypes;
string sql = "update CheckRecord set cPhoto='" + url + "' where ID='" + CheckRecordId + "'";
SqlConnection cnn = new SqlConnection(ConnStr);
SqlCommand cmmd = new SqlCommand(sql, cnn);
cmmd.CommandType = CommandType.Text;
cnn.Open();
int y = cmmd.ExecuteNonQuery();
cnn.Close();
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return false;//"文件上传异常";
}
return true;//"文件上传成功";
}
作者:欧宏远
博客:http://www.cnblogs.com/pdasoft
日期:2011年12月7日