1. 数据类型
基本数据类型: 整型(int short long float double)字符型(char) 字符串(string) decimal(这个类型可以根据输入的数来定义自己是什么整型)
无符号类型 : uint、就是基本类型前面加u
注意:如果是 无符号整型,那么如果内容溢出,就会归零 不会溢出
2. 请求类型
在html设置发送方式,默认get方式
try
{
var userName = context.Request["acount"];
var userPassword = context.Request["pwd"];
Convert.ToInt32(userName);
Convert.ToInt32(userPassword);
}
catch (Exception)
{
context.Response.Write("输入信息格式错误,请仔细核对");
}
context.Response.Write("Hello World");
context.Response.End(); //停止程序
return; //停止程序(常用)
3. 画布的设置
context.Response.ContentType = "text/Image";
*********************************
using (Bitmap bmp = new Bitmap(500, 500)) //创建一个尺寸为500*500的内存图像
{
HttpRequest request = context.Request;
using (Graphics grap = Graphics.FromImage(bmp))//得到图片的画布
{
using (Font font = new Font(FontFamily.GenericSerif, 30))
{
grap.DrawString(image + "nice to meet you", new Font(FontFamily.GenericSerif, 30), Brushes.Red, 0, 0);
//grap.DrawString("浏览器:" + request.Browser.Browser + request.Browser.Version, font, Brushes.Red, 0, 50);
grap.DrawString("操作系统:" + request.Browser.Platform, font, Brushes.Red, 0, 50);
bmp.Save(@"G:\source\Asp.NETashx\2.png", ImageFormat.Png);
}
}
}
4.文件上传
1)在form标签 设置属性 method = “post” enctype=“multipart/form-data”
2)ashx文件中,设置获取文件上传信息的对象 HttpPostFile file =context.Request.Files[“file”]
3)上传后,将上传的文件 保存:记得设置保存路径: string location = context.Server.MapPath("~/upload") + “/” + file.FileName;
file.SaveAs(location);
<input type="file" name="file" /> <br/>
<input type="submit" value="上传"/>
</form>
********************
context.Response.ContentType = "text/html";
HttpPostedFile file = context.Request.Files["file"]; //或许Html发送过来的文件
if(file.contentLength <=0)
{
context.Response.Write("请选择要上传的文件");
return;
}
if (file.ContentLength > 1024 * 1024)
{
context.Response.Write("文件不能大于1M");
return;
}
if (file.ContentType != "image/jpeg" && file.ContentType != "image/gif")
{
context.Response.Write("文件类型不允许");
return;
}
string location = context.Server.MapPath("~/upload") + "/" + file.FileName;
file.SaveAs(location);
context.Response.Write(location);