123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace MicroPhotoShare
- {
- public partial class UploadPhoto : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- if (Request.QueryString["yuname"] != null)
- {
- string yuname = LYFZ.WinAPI.SDKSecurity.Decode(Request["yuname"].ToString().Trim().Replace(" ", "+"));
- string saveName = Request.QueryString["filename"].ToString();
- UploadPhotoFile(yuname, saveName);
- }
- else
- {
- Response.Write("Error");
- }
- }
- }
- private void UploadPhotoFile(string yuname, string saveName)
- {
- string DirectoryPath = GetUploadPath(yuname);
- if (!System.IO.Directory.Exists(DirectoryPath))
- {
- //System.Security.AccessControl.DirectorySecurity dsec= new System.Security.AccessControl.DirectorySecurity();
- System.IO.Directory.CreateDirectory(DirectoryPath);
- }
- foreach (string f in Request.Files.AllKeys)
- {
- HttpPostedFile file = Request.Files[f];
- if (UploadFile(file, DirectoryPath + "\\" + saveName))
- {
- Response.Write("Success");
- }
- else {
- Response.Write("Error:" + file.FileName);
- }
-
- }
- }
- public bool ExistsImgage(string fileName)
- {
- if (fileName.ToLower().EndsWith(".jpg")
- || fileName.ToLower().EndsWith(".jpge")
- || fileName.ToLower().EndsWith(".png")
- || fileName.ToLower().EndsWith(".gif"))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public bool UploadFile(HttpPostedFile file, string savePath)
- {
- try
- {
- if (ExistsImgage(file.FileName))
- {
- file.SaveAs(savePath);
- return true;
- }
- else
- {
- return false;
- }
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 获取上传路径
- /// </summary>
- /// <param name="childFolderPath">子目录路径</param>
- /// <returns></returns>
- public string GetUploadPath(string childFolderPath)
- {
- return Server.MapPath("~/UploadFolder/" + childFolderPath);
- }
- }
- }
|