上传文件时出现问题 [英] Problem in uploading a file
问题描述
使用此代码我正在上传文件并在gridview上显示。但是当我点击上传按钮时没有任何事情发生,没有错误来....
我选择文件时通过调试检查HttpFileCollection的对象没有任何内容意味着hfc给出null。
请帮帮我
Using this code I''m uploading the file and showing on gridview. But when I click the upload button nothing happen and no error come....
I have checked by debugging when I select the file nothing comes to the object of HttpFileCollection means hfc gives null.
Please help me
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpFileCollection hfc = Request.Files; // htc gives null
foreach (string filetagname in hfc)
{
HttpPostedFile hpf = Request.Files[filetagname];
if (hpf.ContentLength > 0)
{
int size = hpf.ContentLength;
string name = hpf.FileName;
int position = name.LastIndexOf(@"\\");
name = name.Substring(position + 1);
string content = hpf.ContentType;
byte[] filedata = new byte[size];
hpf.InputStream.Read(filedata, 0, size);
filedownload.savefile(name, content, size, filedata);
}
}
DataTable dt = filedownload.GetFileList();
GridView1.DataSource = dt;
GridView1.DataBind();
}
推荐答案
嘿见 HttpRequest.Files Property [ ^ ]
它显示了使用此功能的一个很好的例子,希望这有帮助。
Hey see HttpRequest.Files Property[^]
It shows a good example of using this functionality, Hope this helps.
按照答案上传多个文件aspx.net C# [ ^ ]。
Follow the answer Upload more than one file aspx.net C#[^].
据我所见,您只需要在表单中添加enctype =multipart / form-data: br />
您没有使用asp:FileUpload控件自动添加该类型,因此您应该手动执行此操作。
As far as I can see, you just need to add enctype="multipart/form-data" to your form:
You are not using asp:FileUpload control which adds that enctype automatically, so you should do that manually.
谢谢......
Thanks...
这篇关于上传文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!