try
{
HttpPostedFile file = HttpContext.Current.Request.Files["uploadfile"];
if (file != null)
{
//檔案擴充類型判斷
if (IsAllowedExtension(file))
{
string returnurl = UpLoad(file);
if (string.IsNullOrEmpty(returnurl))
{
context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = "上傳失敗" }));
}
else
{
context.Put("json", LitJson.JsonMapper.ToJson(new { code = 0, data = returnurl }));
}
}
else
{
context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = "檔案格式不正确" }));
}
}
else
{
context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = "檔案對象為null" }));
}
}
catch (Exception e)
{
context.Put("json", LitJson.JsonMapper.ToJson(new { code = 1, msg = e.Message }));
}
private bool IsAllowedExtension(HttpPostedFile hifile)
{
bool ret = false;
String newFile = System.Web.HttpContext.Current.Server.MapPath("~/" + System.Guid.NewGuid().ToString("D") + ".tmp");
hifile.SaveAs(newFile);
System.IO.FileStream fs = new System.IO.FileStream(newFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
string fileclass = "";
byte buffer;
try
{
buffer = r.ReadByte();
fileclass = buffer.ToString();
buffer = r.ReadByte();
fileclass += buffer.ToString();
}
catch
{
return false;
}
r.Close();
fs.Close();
/*檔案擴充名說明
*7173 gif
*255216 jpg
*13780 png
*6677 bmp
*239187 txt,aspx,asp,sql
*208207 xls.doc.ppt
*6063 xml
*6033 htm,html
*4742 js
*8075 xlsx,zip,pptx,mmap,zip
*8297 rar
*01 accdb,mdb
*7790 exe,dll
*5666 psd
*255254 rdp
*10056 bt種子
*64101 bat
* PDF = 3780,
*/
String[] fileType = { "255216", "7173", "6677", "13780", "8297", "5549", "208207","8075","207208","3780" };
for (int i = 0; i < fileType.Length; i++)
{
if (fileclass == fileType[i])
{
ret = true;
break;
}
}
System.IO.File.Delete(newFile);
return ret;
}
protected string uploadFiles(HttpPostedFile userPostedFile)
{
string fileName, fileExtension;//檔案名,檔案類型
fileName = System.IO.Path.GetFileName(userPostedFile.FileName);
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
int FileLen = userPostedFile.ContentLength;
Byte[] FileData = new Byte[FileLen];
Stream sr = userPostedFile.InputStream;//建立資料流對象
sr.Read(FileData, 0, FileLen);
sr.Close();
WebReference.PicUpload pu = new WebReference.PicUpload();
//localhost.PicUpload pu = new localhost.PicUpload();
string currentpath = "/upfiles/"; //這是上傳後的圖檔路徑
//string filepath = Server.MapPath(currentpath); //檔案夾名,以日期為檔案夾名,圖檔所在這個地方
string filepath = "D:\\dzt\\CommodityPic\\";
//HttpPostedFile userPostedFile = Request.Files["File1"];
if (FileLen > 0 && FileLen / 1024 < 8192)
{
string file_name = System.DateTime.Now.ToString("yyyyMMddHHmmssfff"); //新檔案名
string sPath = "MerchantPic"; //建立日期檔案夾
string file_ext = System.IO.Path.GetFileName(userPostedFile.FileName).Split(\'.\')[1];
string ext = userPostedFile.ContentType; //擷取檔案類型
if (ext == "image/pjpeg" || ext == "image/jpeg" || ext == "image/png" || ext == "image/jpg")
{
string sPath2 = filepath + sPath;
//if (!Directory.Exists(sPath2)) //判斷日期檔案夾是否存在
//{
// Directory.CreateDirectory(sPath2);//建立日期檔案夾
//}
string fpath = filepath + sPath + "\\" + file_name + "." + file_ext;
if (File.Exists(fpath))
{
file_name = file_name + "1";
fpath = filepath + sPath + "\\" + file_name + "." + file_ext;
}
string uploadpath = sPath + "/" + file_name + "." + file_ext;
bool result = pu.uploadFiles(FileData, FileLen, fpath, sPath2);
//userPostedFile.SaveAs(fpath);
if (result)
{
return uploadpath;
}
else
{
return "";
}
}
else
{
return "1";
}
}
else
{
return "2";
}
//string currentpath = "/upfiles/"; //這是上傳後的圖檔路徑
////string filepath = Server.MapPath(currentpath); //檔案夾名,以日期為檔案夾名,圖檔所在這個地方
//string filepath = "E:\\dzt\\CommodityPic\\";
////HttpPostedFile userPostedFile = Request.Files["File1"];
//if (userPostedFile.ContentLength / 1024 > 0 && userPostedFile.ContentLength / 1024 < 8192)
//{
// string file_name = System.DateTime.Now.ToString("yyyyMMddHHmmssfff"); //新檔案名
// string sPath = "MerchantPic"; //建立日期檔案夾
// string file_ext = System.IO.Path.GetFileName(userPostedFile.FileName).Split(\'.\')[1];
// string ext = userPostedFile.ContentType; //擷取檔案類型
// if (ext == "image/pjpeg" || ext == "image/jpeg" || ext == "image/png" || ext == "image/jpg")
// {
// string sPath2 = filepath + sPath;
// if (!Directory.Exists(sPath2)) //判斷日期檔案夾是否存在
// {
// Directory.CreateDirectory(sPath2);//建立日期檔案夾
// }
// string fpath = filepath + sPath + "\\" + file_name + "." + file_ext;
// if (File.Exists(fpath))
// {
// file_name = file_name + "1";
// fpath = filepath + sPath + "\\" + file_name + "." + file_ext;
// }
// string uploadpath = sPath + "/" + file_name + "." + file_ext;
// userPostedFile.SaveAs(fpath);
// return uploadpath;
// }
// else
// {
// return "1";
// }
//}
//else
//{
// return "2";
//}
}
public bool uploadFiles(byte[] FileByteArray, int FileLength, string SaveToUrl,string sPath2)
{
try
{
if (!Directory.Exists(sPath2)) //判斷日期檔案夾是否存在
{
Directory.CreateDirectory(sPath2);//建立日期檔案夾
}
FileStream fs = new FileStream(SaveToUrl, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(FileByteArray, 0, FileLength);
fs.Close();
}
catch(Exception ex)
{
LogUtils.WriteLog(LogUtils.LogType.ERROR, ex.Message);
return false;
}
return true;
}