从html页面Post附件到aspx接收页
客户端例子:
<form id="fResAtt" name="fResAtt" enctype="multipart/form-data" action="http://bill.xxxx.net/UploadResAttService.aspx" method="post" target="_blank">
<input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" value="" />
<input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type="hidden" value="" />
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var fr = document.getElementById('fResAtt');
if (fr.onsubmit == null || fr.onsubmit())
{
fr.__EVENTTARGET.value = eventTarget;
fr.__EVENTARGUMENT.value = eventArgument;
fr.submit();
}
}
//-->
</script>
<input id="resumeID" name="resumeID" type="hidden" value="C212CFB7-1675-445D-8101-A593E99EE126" />
<input id="attFile" name="attFile" type="file" />
<input id="submitAtt" name="submitAtt" type="button" value="提交" onclick="__doPostBack('', '');" />
</form>
<input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" value="" />
<input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type="hidden" value="" />
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument)
{
var fr = document.getElementById('fResAtt');
if (fr.onsubmit == null || fr.onsubmit())
{
fr.__EVENTTARGET.value = eventTarget;
fr.__EVENTARGUMENT.value = eventArgument;
fr.submit();
}
}
//-->
</script>
<input id="resumeID" name="resumeID" type="hidden" value="C212CFB7-1675-445D-8101-A593E99EE126" />
<input id="attFile" name="attFile" type="file" />
<input id="submitAtt" name="submitAtt" type="button" value="提交" onclick="__doPostBack('', '');" />
</form>
此hmtl页包含一下类型为file的input,此数据会被post到另一个aspx接引页,提交时定义一个__doPostBack的js函数,在函数中使form进行submit()。
接收的aspx页
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(GetValue("resumeID")))
{
ResumeAttachmentConfiguration attConfig = JobConfiguration.GetConfig().ResumeAttachmentConfiguration;
Guid resumeID = new Guid(GetValue("resumeID"));
HttpPostedFile postedFile = Request.Files[0];
//上传文件类型限制
bool allowedType = false;
ArrayList typeList = attConfig.GetAllowedAttachmentTypeList();
string[] extensions = postedFile.FileName.Split('.');
string fileExtension = extensions[extensions.Length - 1].ToString();//获取上传文件名的扩展名
foreach (string attType in typeList)
{
if (attType.Trim().ToLower() == fileExtension.ToLower())
{
allowedType = true;
break;
}
}
if (!allowedType)
{
OutputErrorMessage("上传的文件类型不在允许范围之内");
}
// 上传文件大小限制
if (postedFile.ContentLength > attConfig.MaxAttachmentSize * 1000)
{
OutputErrorMessage("上传的附件大小超出允许范围");
}
//添加简历附件
ResumeAttachment att = new ResumeAttachment(postedFile, resumeID);
try
{
ResumeAttachments.AddAttachment(att);
}
catch (Exception ex)
{
OutputErrorMessage(ex.Message);
}
OutputSusscessMessage();
}
OutputErrorMessage("请提供简历UID");
}
private string GetValue(string name)
{
return Request.Form.Get(name);
}
private void OutputMessage(string msg)
{
Response.Output.Write(msg);
Response.End();
}
private void OutputErrorMessage(string msg)
{
OutputMessage("flase:" + msg);
}
private void OutputSusscessMessage()
{
OutputMessage("true:" + "成功添加了简历附件");
}
{
if (!String.IsNullOrEmpty(GetValue("resumeID")))
{
ResumeAttachmentConfiguration attConfig = JobConfiguration.GetConfig().ResumeAttachmentConfiguration;
Guid resumeID = new Guid(GetValue("resumeID"));
HttpPostedFile postedFile = Request.Files[0];
//上传文件类型限制
bool allowedType = false;
ArrayList typeList = attConfig.GetAllowedAttachmentTypeList();
string[] extensions = postedFile.FileName.Split('.');
string fileExtension = extensions[extensions.Length - 1].ToString();//获取上传文件名的扩展名
foreach (string attType in typeList)
{
if (attType.Trim().ToLower() == fileExtension.ToLower())
{
allowedType = true;
break;
}
}
if (!allowedType)
{
OutputErrorMessage("上传的文件类型不在允许范围之内");
}
// 上传文件大小限制
if (postedFile.ContentLength > attConfig.MaxAttachmentSize * 1000)
{
OutputErrorMessage("上传的附件大小超出允许范围");
}
//添加简历附件
ResumeAttachment att = new ResumeAttachment(postedFile, resumeID);
try
{
ResumeAttachments.AddAttachment(att);
}
catch (Exception ex)
{
OutputErrorMessage(ex.Message);
}
OutputSusscessMessage();
}
OutputErrorMessage("请提供简历UID");
}
private string GetValue(string name)
{
return Request.Form.Get(name);
}
private void OutputMessage(string msg)
{
Response.Output.Write(msg);
Response.End();
}
private void OutputErrorMessage(string msg)
{
OutputMessage("flase:" + msg);
}
private void OutputSusscessMessage()
{
OutputMessage("true:" + "成功添加了简历附件");
}
HttpPostedFile postedFile = Request.Files[0];方法提取了第一个Post过来的附件。