context.Request.Files [0]在FireFox中为空,

时间:2011-12-14 06:42:46

标签: c# asp.net firefox httpcontext

我想提交上传

代码是这样的:

int iTotal = context.Request.Files.Count;   
if(iTotal>0)
  //Upload()....

使用IE7,8,9时工作正常 但是当我在FireFox 8中使用它时,它不再起作用了。
iTotal始终等于0。

对我有什么想法/建议吗? 编辑:

我有两页。在第A页

 $("idBtnupload").onclick = function()
   { 
     ... 
    fu.Form.submit(); 
    } 
<form id="uploadForm" action="File.ashx?type=<% =type %>" method="post" enctype="multipart/form-data"> 
    <input type="button" value="开始上传" id="idBtnupload" /> 
</form>  

2 个答案:

答案 0 :(得分:0)

您必须在输入文件中包含"runat='server'"属性

示例:

 <input type="file" id="myfile1" runat="server" >

答案 1 :(得分:0)

String ffFileName = HttpContext.Current.Request.Headers["X-File-Name"];

if ((null == ffFileName) && (0 == context.Request.Files.Count))
  return;

string tempDir = ConfigurationSettings.AppSettings["FilesTempDir"];
string filePath = String.Format("{0}{1}", tempDir, Guid.NewGuid().ToString());

if (null != ffFileName)
{
  Stream inputStream = HttpContext.Current.Request.InputStream;
  byte[] fileBytes = ReadFully(inputStream);

  File.WriteAllBytes(filePath, fileBytes);
}
else
{
  HttpPostedFile file = context.Request.Files[0];
  file.SaveAs(filePath);

}

context.Response.ContentType = "text/html";
context.Response.Write("{\"success\": true}");

这是读取流的方法

public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
  int read;
  while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
  {
    ms.Write(buffer, 0, read);
  }
  return ms.ToArray();
}

}