同桌上课用手指进去了好爽_欧美丰满熟妇xxⅹⅹ性大i_成人av天天日天天拍拍_猛男gay帅男gay男男同志_欧美va天堂在线观看_人妻无码av中文系列三里桃花_亚欧免费无码在线观看_久久久精品国产亚洲av水_日韩在线免费看污污污_2021无码专区人妻系列日韩

首頁 優(yōu)化推廣 FileUpload.HasFile 屬性

FileUpload.HasFile 屬性

來源: | 時間:2011/5/2 7:31:16 |

獲取一個值,該值指示 FileUpload 控件是否包含文件。

屬性值

類型:System.Boolean
如果 FileUpload 包含文件,則為 true;否則為 false。

 

HasFile 屬性獲取一個值,該值指示 FileUpload 控件是否包含要上載的文件。 在對要上載的文件執(zhí)行操作之前,使用該屬性來驗證該文件是否存在。 例如,在調用 SaveAs 方法將文件保存到磁盤之前,使用 HasFile 屬性來驗證文件存在。 如果 HasFile 返回 true,則調用 SaveAs 方法。 如果它返回 false,則向用戶顯示消息,指示控件不包含文件。

下面的示例演示如何創(chuàng)建執(zhí)行錯誤檢查的 FileUpload 控件。 在保存文件之前,調用 HasFile 方法來驗證該控件是否包含要上載的文件。 此外,還調用 File.Exists 方法來檢查路徑中是否已存在同名的文件。 如果存在同名文件,則在調用 SaveAs 方法之前為要上載的文件的名稱前加上一個下劃線字符。 這可以防止現(xiàn)有文件被覆蓋。

 
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>FileUpload.SaveAs Method Example</title>
<script runat="server">

    protected void  UploadButton_Click(object sender, EventArgs e)
    {
        // Before attempting to save the file, verify
        // that the FileUpload control contains a file.
        if (FileUpload1.HasFile)
          // Call a helper method routine to save the file.
          SaveFile(FileUpload1.PostedFile);
        else
          // Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file to upload.";
    }

      void SaveFile(HttpPostedFile file)
      {           
        // Specify the path to save the uploaded file to.
        string savePath = "c:\\temp\\uploads\\";

        // Get the name of the file to upload.
        string fileName = FileUpload1.FileName;

        // Create the path and file name to check for duplicates.
        string pathToCheck = savePath + fileName;

        // Create a temporary file name to use for checking duplicates.
        string tempfileName = "";

        // Check to see if a file already exists with the
        // same name as the file to upload.       
        if (System.IO.File.Exists(pathToCheck))
        {
          int counter = 2;
          while (System.IO.File.Exists(pathToCheck))
          {
            // if a file with this name already exists,
            // prefix the filename with a number.
            tempfileName = counter.ToString() + fileName;
            pathToCheck = savePath + tempfileName;
            counter ++;
    

服務熱線

153 8323 9821

功能和特性

價格和優(yōu)惠

網站和維護

推廣和優(yōu)化

微信服務號