C# 上载文件时出现“找不到文件”错误

C# 上载文件时出现“找不到文件”错误,c#,asp.net,custom-controls,C#,Asp.net,Custom Controls,我的web应用程序中有一个自定义控件,用于上载多个文件。用户首先选择他的文件,然后按下上载按钮。当用户系统中不存在该文件时,页面无法加载,并显示错误“找不到文件”。我怎样才能抓住这个错误?因为用户可以上传多个文件,如果一个文件不存在,我想显示错误消息并处理其他文件。这是我的密码 for (int i = 0; i < files.Count; i++) { if (!Directory.Exists(Server.MapPath("~/files/"))) {

我的web应用程序中有一个自定义控件,用于上载多个文件。用户首先选择他的文件,然后按下上载按钮。当用户系统中不存在该文件时,页面无法加载,并显示错误“找不到文件”。我怎样才能抓住这个错误?因为用户可以上传多个文件,如果一个文件不存在,我想显示错误消息并处理其他文件。这是我的密码

for (int i = 0; i < files.Count; i++)
{
    if (!Directory.Exists(Server.MapPath("~/files/")))
    {
         Directory.CreateDirectory(Server.MapPath("~/files/"));
    }

    HttpPostedFile file = files[i];
    if (!string.IsNullOrEmpty(file.FileName))
    {
        if (file.ContentLength <= 209715200)
        {
            var c = save.NameSave(file);
            fi.path = c;
            fi.title = file.FileName;
            userfiles.Add(fi);
        }
    }   
for(int i=0;i如果(file.ContentLength您可以获取用户选择的所有文件路径的名称和数组,然后迭代如下内容

for(int i = 0 ; i < arr.length ; i++)
 {
    if(!File.Exists(arr[i]))
      {
         //your file do not exist do what ever you want here;
      }
     else
      {
        //your file exists your code for remaining files that exists goes here!
      }
 }
for(int i=0;i
浏览您上载的每个文件: 下面的代码检查目录是否存在,并提供文件写入和读取权限

foreach (string file in context.Request.Files)
            {
                HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile;
                string rootPathForwritingfile=AppDomain.CurrentDomain.BaseDirectory +"your destination folder name//"+Uri.UnescapeDataString(Path.GetFileName(hpf.FileName));
                //check for the directory exists or not
                FileStream fileStream = new FileStream(rootPathForwritingfile, FileMode.Create, FileAccess.ReadWrite);
                ReadWriteStream(hpf.InputStream, fileStream);

            }

private static void ReadWriteStream(Stream readStream, Stream writeStream)
        {
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            // write the required bytes
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();
        }
**

用于在上载之前使用检查文件是否存在的已编辑答案 Javascript:

**

$('#')。单击(函数(e){
$.each($('#File1')。文件,函数(索引,文件){
if(文件类型!==“未定义”&&file.size>0)
{
警惕(“成功”);
//做你的事
}
其他的
{
警报(“未找到文件”);
//完成破坏事件的工作,并警告用户未找到指定的文件。
//试试e.preventdefault();
}
});
});

当文件不存在时,代码不会执行,我在代码中添加断点,但它根本没有输入代码。如果您提供完整的代码就太好了。我加载了我的网页设计。![Valid XHTML]()。按“添加更多文件”,用户可以选择多个文件,按“进程”,开始处理文件。当文件在本地计算机中不存在时,当按下“处理”按钮时,显示“未找到文件”。我的问题是,当文件在本地计算机中不存在时,该页不执行任何代码。我使用了断点,当加载该页时,它没有进入代码隐藏状态。@mansureh您可以发布用于上载文件的HTML代码吗。
$('#<%=btnUpload.ClientId%>').click(function(e){
  $.each($('#File1').files,function(index,file){
     if(type of file !== 'undefined' && file.size > 0)
      {
        alert('success');
        //do your stuff
      }
     else
      {
        alert('file not found');
        //do your stuff for breaking the event and warn the user that the file specified was not found.
        //try e.preventdefault();
      }
});
});