2014-09-06 187 views
-1

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

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); 
     } 
    } 
+0

如果您提供完整的代码,那将会很棒。 – RajeshKannan 2014-09-06 09:03:38

+0

我加载了我的网页设计。 ![有效的XHTML](http://i57.tinypic.com/2qsrmvk.png)。通过按“添加更多文件”,用户可以选择多个文件,按“处理”键,即可启动处理文件。当本地计算机中不存在文件时,按“处理”键时,显示“未找到文件”。 – mansureh 2014-09-06 09:26:51

回答

0

你可以和由用户选择的所有文件路径的数组,然后遍历一些这样的事

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! 
     } 
} 
+0

当文件不存在时,代码不会执行,我在代码中放置了断点,但它并没有输入代码中。 – mansureh 2014-09-06 08:11:07

0

通过自己上传的每个文件转至: 以下代码检查目录是否存在,并提供文件写入和读取权限。

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文件之前存在编辑答案:

**

$('#<%=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(); 
     } 
}); 
}); 
+0

我的问题是当本地计算机中不存在文件时,页面不执行任何代码。我使用了断点,并且在页面加载时,它没有进入代码。 – mansureh 2014-09-06 09:09:01

+0

@mansureh您可以发布您用于上传文件的HTML代码吗? – RajeshKannan 2014-09-06 13:33:00

+0

我在答案中添加了我的html代码。 – mansureh 2014-09-07 03:37:57

0

文件上传我的html代码:

<td class="style4" dir="rtl" align="right" 
      style="border: thin ridge #3399FF; direction: ltr; margin-left: 27px;" > 
      &nbsp; 
      <p id="upload-area" style=" direction: ltr; margin-left: 27px;" align="left" > 
<span>File 1 : </span><input id="File1" type="file" size="60" maxlength="209715200" tabindex="0" runat="server" /> 

</p> 
<span id="msg" runat="server"></span> 
<input id="btnAddMore" type="button" value="افزودن فایلهای بیشتر" 
onclick="add()" 
       dir="rtl" 
       style="font-family: Tahoma; font-size: small; background-color: #CC9900;" /> 
      <br /> 
<asp:Button ID="btnUpload" runat="server" 
      Text="نمایه سازی" onclick="btnUpload_Click" BackColor="#00CC00" 
       Font-Names="Tahoma" Font-Size="10pt" Width="170px" /> 
      </td> 


<script type="text/javascript"> 
    function add() { 
     if (!document.getElementById || !document.createElement) 
      return false; 

     var uploadArea = document.getElementById("upload-area"); 

     if (!uploadArea) 
      return; 

     var newLine = document.createElement("br"); 
     uploadArea.appendChild(newLine); 

     if (!add.lastAssignedId) 
      add.lastAssignedId = 2; 

     //---------------------------------------------------- 
     // create new span for fileupload 
     var fuSpan = document.createElement('span'); 
     var fuSpanText = document.createTextNode('File ' + add.lastAssignedId + ' : '); 
     fuSpan.appendChild(fuSpanText); 
     // create new fileupload control 
     var newUploadBox = document.createElement('input'); 
     // set property for input (fileupload) 
     newUploadBox.type = 'file'; 
     newUploadBox.size = '60'; 
     // set other att 
     newUploadBox.setAttribute('id', 'FileUpload' + add.lastAssignedId); 
     newUploadBox.setAttribute('name', 'FileUpload:' + add.lastAssignedId); 
     newUploadBox.setAttribute('maxlength', '209715200'); 
     newUploadBox.setAttribute('tabindex', add.lastAssignedId); 

     //---------------------------------------------------- 
     // create new span for separator 
     var seSpan = document.createElement('span'); 
     var seSpanText = document.createTextNode(' | '); 
     seSpan.appendChild(seSpanText); 

     //---------------------------------------------------- 

     // create new textbox for title 
     var newTextBox = document.createElement('input'); 
     // set property for input (textbox) 
     newTextBox.type = 'text'; 
     newTextBox.size = '40'; 
     // set other att 
     newTextBox.setAttribute('id', 'txt' + add.lastAssignedId); 
     newTextBox.setAttribute('name', 'txt' + add.lastAssignedId); 
     newTextBox.setAttribute('tabindex', add.lastAssignedId + 1); 

     //---------------------------------------------------- 

     uploadArea.appendChild(fuSpan); 
     uploadArea.appendChild(newUploadBox); 



     add.lastAssignedId++; 
    } 
function btnAddMore_onclick() { 

} 



</script>