我有 android 应用程序Http POST
向页面发出请求asp.net
。
我想实现接收此请求的 asp.net 页面并将其内容保存到文件中(这是文件上传)。
给我样品将不胜感激
这是我在asp.net
接收图像的应用程序中的操作方法Android
,关于我收到的简单参数
context.Request["Yourparameter"]
public class RecieveMail : IHttpHandler
{
private string _emailAdressTo;
private string _imageUrl;
private EmailFactory _emailFactory;
public void ProcessRequest(HttpContext context)
{
ImageFactory factory = new ImageFactory(context);
try
{
_imageUrl = factory.SaveImage("uploaded");
if (string.IsNullOrEmpty(context.Request["from"]) || string.IsNullOrEmpty(context.Request["to"])) return;
_emailFactory = new EmailFactory(_imageUrl);
if (_emailFactory.SendMail(context.Request["from"], context.Request["to"]))
context.Response.Write(!factory.DeleteImage(_imageUrl) ? "Email sent" : "Image deleted");
}
catch (Exception ex)
{
context.Response.Write(" error converting " + ex.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
// 这里是图像保存工厂
public class ImageFactory
{
private readonly HttpContext _context;
private Image _image;
public ImageFactory(HttpContext context)
{
_context = context;
}
/// <summary>
/// Get image by name from post
/// </summary>
/// <param name="ctxImageParamName">image that have been posted in email</param>
/// <returns></returns>
public string SaveImage(string ctxImageParamName)
{
if (string.IsNullOrEmpty(_context.Request[ctxImageParamName])) return null;
string url = GenerateImagePath("FrameMe");
ByteArrayToImageAndSave(Decode(_context.Request[ctxImageParamName]), url);
return url;
}
public bool DeleteImage(string url)
{
try
{
if (!File.Exists(url)) return false;
File.Delete(url);
return true;
}
catch (Exception ex)
{
return false;
}
}
private string GenerateImagePath(string directory)
{
return _context.Server.MapPath(string.Format("~/Images/{1}/Image1_{0}.jpg", DateTime.Now.ToString("ddMMyyyyHHmmss"), directory));
}
private void ByteArrayToImageAndSave(byte[] byteArrayIn, string imageUrl)
{
try
{
MemoryStream ms = new MemoryStream(byteArrayIn);
_image = Image.FromStream(ms);
_image.Save(imageUrl, new ImageFormat(Guid.NewGuid()));
_image.Dispose();
}
catch (Exception ex)
{
_image = null;
}
}
private byte[] Decode(string str)
{
return Convert.FromBase64String(str);
}
}
使用 asp.net 中的 Request 对象将发布的信息检索到页面。
例如:
Request.Form["NameOfVar"]