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

发布于 2024-12-21 00:48:56 字数 562 浏览 1 评论 0原文

我想上传文件

代码是这样的:

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>  

I want to file Upload

The code is like this :

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

It works fine when I use IE7,8,9
But when I use it in FireFox 8 , it doesn't work anymore.
iTotal always equal 0.

Are there any ideas/suggestions to me?
EDIT:

I have two page. In page 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>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-12-28 00:48:56

您必须在输入文件

示例中包含 "runat='server'" 属性:

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

You have to include "runat='server'" attribute to your input file

example:

 <input type="file" id="myfile1" runat="server" >
信仰 2024-12-28 00:48:56
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();
}

}

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}");

This is method for read stream

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();
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文