上传文件

if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files[0];
if (file.ContentLength > 0)
{
//string filename = System.IO.Path.GetFileName(file.FileName);
//string path = context.Request.MapPath("upload/" + filename);
if (file.ContentType == "image/jpeg")
{
string ext = System.IO.Path.GetExtension(file.FileName);

                //唯一的文件名
                Random random = new Random();
                string filename = DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(1000, 10000) + ext;
                string path = context.Request.MapPath("upload/" + filename);

                file.SaveAs(path);
            }
            else
            {
                context.Response.Write("shou jian");
            }
        }
        else
        {
            context.Response.Write("请选择文件");
        }
    }

posted on 2015-02-19 15:16  木屐  阅读(91)  评论(0编辑  收藏  举报

导航