c\#웹 fileUpload 단일 파일 업로드 실현

1)프론트 코드
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="VSFileUpLoad._Default" %>
<!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 runat="server">
    <title></title>
    <script language ="javascript" type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script language="javascript" type="text/javascript">
        function isCoverFile() {
            if (confirm("    ,    ?")) {
                $.ajax({
                    type: "POST",   //  WebService  Post    
                    contentType: "application/json",
                    url: "Default.aspx/Cover",
                    dataType: 'json',
                    success: function(result) {//    ,result,   
                        if (result.d == true) {
                            alert("    !");
                        }
                        else {
                            alert("    !");
                        }
                    }
                });
            }
        }
        function uploadSuccess(){//    
            alert("    !");
        }
        function uploadError() {//    
            alert("    !");
        }
        function fileIsNull(){//      
            alert("       !");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="ContentPanel" runat="server"></asp:Panel>
        <asp:Button ID="btnFileUpload" runat="server" Text="  " 
            onclick="btnFileUpload_Click" />
    </div>
    </form>
</body>
</html>

2)배경 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Services;
namespace VSFileUpLoad
{
    public partial class _Default : System.Web.UI.Page
    {
        private static FileUpload FileUpload2;
        private static string filePath="";
        protected void Page_Load(object sender, EventArgs e)
        {
            FileUpload2 = new FileUpload();
            FileUpload2.ID = "FileUpload2";
            this.FindControl("ContentPanel").Controls.Add(FileUpload2);
        }
        protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload2.HasFile)//      
            {
                string savePath = Server.MapPath(@"~/pic");//     
                if (!Directory.Exists(savePath))//      
                {
                    Directory.CreateDirectory(savePath);//     
                }
                string fileName = FileUpload2.FileName;//    
                filePath = savePath + @"/" + fileName;//    
                if (File.Exists(filePath))//    
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "fileExist", "isCoverFile();", true);
                }
                else//     
                {
                    try
                    {
                        FileUpload2.SaveAs(filePath);//    
                        Page.ClientScript.RegisterStartupScript(GetType(), "uploadSuccess", "uploadSuccess();", true);
                    }
                    catch(Exception ex)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "uploadError", "uploadError();", true);
                    }
                }
            }
            else //       
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "fileIsNull", "fileIsNull();", true);
            }
        }
        /// <summary>
        ///     
        /// </summary>
        /// <param name="allPath"></param>
        /// <returns></returns>
        [WebMethod]
        public static bool Cover()
        {
            bool flag = false;
            try
            {
                FileUpload2.SaveAs(filePath);//    
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
            }
            return flag;
        }
    }
}

좋은 웹페이지 즐겨찾기