如何上传小于10MB的zip文件 [英] How to upload a zip file with less than 10MB size

查看:97
本文介绍了如何上传小于10MB的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的C#新手.我正在开发一个页面,以将zip文件上传到小于10MB的服务器.现在,我可以将zip文件上传到服务器,但是仍然无法验证文件类型(仅允许zip文件)和文件大小.


这是我的代码:

Hi I am a C# fresher to C#. I am developing a page to upload a zip file to server with less than 10MB. Now i am able to upload the zip file to server, but still unable to validate the file type(only allow zip file) and file size.


here is my code:

<pre lang="xml"><system.web><br />
    <httpRuntime maxRequestLength="10240" executionTimeout="1500"/><br />
  </system.web></pre><br />





<br />
<pre lang="xml"><form id="form1" method="post" enctype="multipart/form-data" runat="server"><br />
<br />
    <input type="file" id="File1" name="File1" /><br />
    <br/><br />
    <input type="submit" value="Upload Data File" name="cmdSubmit"/><br />
    </form></pre><br />




背后的代码:




Behind code:

<pre lang="cs">protected void Page_Load(object sender, EventArgs e)<br />
       {<br />
           foreach (string key in Request.Files.Keys)<br />
           {<br />
               HttpPostedFile file = Request.Files.Get(key);<br />
               string fn = Path.GetFileNameWithoutExtension(file.FileName) +<br />
               DateTime.Now.ToString("ddMMyyyy") + Path.GetExtension(file.FileName);<br />
               file.SaveAs(Server.MapPath("./data/" + fn));<br />
<br />
               Response.Write("<br>" + fn + " is uploaded!");<br />
<br />
           }<br />
<br />
       }</pre><br />


在此先感谢您.


Thanks in advance.

推荐答案

问题的答案在您的代码之内

通过字符串比较检查扩展名
The answer for your question is within your code

check for extension by string comparison
if(string.Compare(Path.GetExtension(file.FileName),".zip",true)!=0)
{
ShowMessage("You Can upload only zip files");
return;
}

void ShowMessage(msg)
{
ClientScript.RegisterStartupScript(GetType(),"msg","alert('"+ msg +"');",true)
}



和国际剑联尺寸



and for fie size

if((file.FileBytes.LongLength/(1024*1024))>10)
{
ShowMessage("You can upload upto 10Mb");
}



--Pankaj



--Pankaj


这篇关于如何上传小于10MB的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆