2014-09-06 184 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>