c\#웹 fileUpload 단일 파일 업로드 실현
<%@ 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;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Fortinet FortiWeb Web Application Firewall Policy BypassFrom: Geffrey Velasquez Date: Wed, 2 May 2012 20:33:23 -0500...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.