上传图片

<div style="display: none">
        <asp:LinkButton ID="liBtnUplodLogo" runat="server" OnClick="liBtnUplodLogo_Click"></asp:LinkButton>
    </div>

<img id="ParkLogo" runat="server" onerror='this.src="http://images.cnblogs.com/defava.jpg"' style="float: left;" />
                        <input id="fileUploadLogo" runat="server" name="fileUploadLogo" style=" auto;
                            height: auto; display: none" type="file" onchange="UploadTempLogo();" />
                        <asp:TextBox ID="txtFilePath" Width="172px" runat="server" Text=""></asp:TextBox>
                        <input type="button" value="浏览图片" style=" 80px" onclick="fn_clickFile();" />
                        <img id="tempImg" runat="server" src="" style=" 100px; height: 50px;" />

      function fn_clickFile() {
            $("#fileUploadLogo").click();
            var filePath = $("#fileUploadLogo").val();
            $("#txtFilePath").val(filePath);
        }

        //上传临时文件
        function UploadTempLogo() {
            __doPostBack('liBtnUplodLogo', '');
        }

  private static string fileUploadLogoValue = string.Empty;
        private static string FileName = string.Empty;
        private static string UploadStr = string.Empty;
        private static string oldFilepath = string.Empty;

    /// <summary>
    /// 上传临时文件
    /// </summary>
    /// <param name="parkId"></param>
    /// <returns></returns>
    private bool UploadTempLogo()
    {
        fileUploadLogoValue = fileUploadLogo.Value;
        var baseFilePath = ConfigurationManager.AppSettings["tempParkLogo"];
        if (string.IsNullOrEmpty(fileUploadLogo.Value))
        {
            Alert("请选择上传图片");
            return false;
        }
        if (!string.IsNullOrEmpty(fileUploadLogo.Value))
        {
            string type = fileUploadLogo.Value.Substring(fileUploadLogo.Value.LastIndexOf(".") + 1).ToLower();
            if (!(type == "jpg" || type == "bmp" || type == "jpeg" || type == "png" || type == "gif"))
            {
                Alert("只能上传后缀名为jpg,bmp,jpeg,gif,png的图片");
                return false;
            }
        }
        var logoUpload = fileUploadLogo.PostedFile;
        FileName = logoUpload.FileName;
        var logoFileName = string.Empty;
        var logoPath = string.Empty;
        var basePath = baseFilePath;
        if (!Directory.Exists(basePath))
        {
            basePath = basePath + "/";
            FileHelper.FolderCreate(basePath);
        }
        else
        {
            basePath = basePath + "/";
        }
        if (!string.IsNullOrEmpty(logoUpload.FileName))
        {
            logoFileName = DateTime.Now.Ticks + "bus" + System.IO.Path.GetExtension(logoUpload.FileName);
            logoPath = basePath + logoFileName;

            logoUpload.SaveAs(logoPath);
            oldFilepath = logoPath;
            tempImg.Src = "http://a.inyyx.com/ParkImage/ParkTempLogo/" + logoFileName;
            txtFilePath.Text = fileUploadLogoValue;
        }

        UploadStr = Path.GetExtension(logoUpload.ToString());
        var loutName = DateTime.Now.Ticks + "logo" + Path.GetExtension(logoUpload.ToString());

        string ppth = baseFilePath + "/";
        if (!Directory.Exists(ppth))
        {
            Directory.CreateDirectory(ppth);
        }

        var largePath = "l.jpg";
        var middlePath = "m.jpg";
        var smallPath = "s.jpg";



        var LPath = ppth + largePath;
        var MPath = ppth + middlePath;
        var SPath = ppth + smallPath;

        ImageHelper.Resize(basePath + logoFileName, LPath, 150, 150, true, false);     //大图路径
        ImageHelper.Resize(basePath + logoFileName, MPath, 80, 80, true, false);      //中图路径
        ImageHelper.Resize(basePath + logoFileName, SPath, 30, 30, true, false);      //小图路径

        return true;
    }

    public void UploadParkLogo(Guid merchantId)
    {
        var baseFilePath = ConfigurationManager.AppSettings["MerchantLogo"];
        if (string.IsNullOrEmpty(fileUploadLogoValue))
        {
            Alert("请选择上传图片");
            return;
        }
        if (!string.IsNullOrEmpty(fileUploadLogoValue))
        {
            string type = fileUploadLogoValue.Substring(fileUploadLogoValue.LastIndexOf(".") + 1).ToLower();
            if (!(type == "jpg" || type == "bmp" || type == "jpeg" || type == "png" || type == "gif"))
            {
                Alert("只能上传后缀名为jpg,bmp,jpeg,gif,png的图片");
                return;
            }
        }

        var logoFileName = string.Empty;
        var logoPath = string.Empty;
        var basePath = baseFilePath;
        if (!Directory.Exists(basePath + merchantId))
        {
            basePath = basePath + merchantId + "/";
            FileHelper.FolderCreate(basePath);
        }
        else
        {
            basePath = basePath + merchantId + "/";
        }

        if (!string.IsNullOrEmpty(fileUploadLogoValue))
        {
            logoFileName = "merchantLogo.jpg";
            logoPath = basePath + logoFileName;
            BaiKeTool.FileHelper.FileCoppy(oldFilepath, logoPath);
        }

        var loutName = DateTime.Now.Ticks + "merchant" + Path.GetExtension(UploadStr);

        string ppl = baseFilePath + merchantId + "/";
        if (!Directory.Exists(ppl))
        {
            Directory.CreateDirectory(ppl);
        }

        var LPath = ppl + "l.jpg";
        var MPath = ppl + "m.jpg";
        var SPath = ppl + "s.jpg";

        ImageHelper.Resize(basePath + logoFileName, LPath, 150, 150, true, false);     //大图路径
        ImageHelper.Resize(basePath + logoFileName, MPath, 80, 80, true, false);      //中图路径
        ImageHelper.Resize(basePath + logoFileName, SPath, 50, 50, true, false);      //中图路径
    }

    protected void liBtnUplodLogo_Click(object sender, EventArgs e)
    {
        UploadTempLogo();
    }

原文地址:https://www.cnblogs.com/Mancy/p/3303309.html