C〓〓〓の最もそろっているアップロードのピクチャーの方法
方法には、画像のサイズ制限、画像のサイズ、ファイルの内容などの判断が含まれています。
このケースはmvcのデモで、一枚の写真のアップロードをサポートします。
このケースはmvcのデモで、一枚の写真のアップロードをサポートします。
public ActionResult Upload()
{
string imgurl = "";
foreach (string key in Request.Files)
{
// file[0]
HttpPostedFileBase file0 = Request.Files[key];
// byte, MIME
Stream stream;
int size = file0.ContentLength / 1024; // KB
if (size > 1024)
{
return Content(ReturnMsg(Enum_Return. , " 1M:", null));
}
byte[] fileByte = new byte[2];//contentLength, , , 。
stream = file0.InputStream;
stream.Read(fileByte, 0, 2);//contentLength,
//
//System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
//int width = image.Width;
//int height = image.Height;
string fileFlag = "";
if (fileByte != null && fileByte.Length > 0)//
{
fileFlag = fileByte[0].ToString() fileByte[1].ToString();
}
string[] fileTypeStr = { "255216", "7173", "6677", "13780" };// jpg,gif,bmp,png
if (fileTypeStr.Contains(fileFlag))
{
string action = Request["action"];
string path = "/uploads/";
switch (action)
{
case "headimage":
path = "headimage/";
break;
case "blogtype":
path = "blogtype/";
break;
}
string fullpath = path UserInfo.userID "/";
if (!Directory.Exists(Server.MapPath(fullpath)))
{
Directory.CreateDirectory(Server.MapPath(fullpath));
}
Request.Files[key].SaveAs(Server.MapPath(fullpath Request.Files[key].FileName));
imgurl = fullpath Request.Files[key].FileName;
}
else
{
return Content(ReturnMsg(Enum_Return. , " :" fileFlag, null));
}
stream.Close();
}
return Content(ReturnMsg(Enum_Return. , " ", imgurl));
}
一般処理プログラム
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
HttpPostedFile _upfile = context.Request.Files["File"];
if (_upfile.ContentLength < 500000)
{
if (string.IsNullOrEmpty(_upfile.FileName))
{
context.Response.Write(" ");
}
string fileFullname = _upfile.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
{
_upfile.SaveAs(HttpContext.Current.Server.MapPath("photo") "\\" dataName "." type);
HttpCookie cookie = new HttpCookie("photo");
context.Response.Write(" ");
}
else
{
context.Response.Write(" :|jpg|gif|bmp|");
}
}
else
{
context.Response.Write(" 500K !");
}
}
以上が本文の全部です。本文の内容は皆さんの学習や仕事に一定の助けをもたらしてくれると同時に、私達を応援してください。