Windows下Apache部署PHP

1、下载和安装Apache服务器:
访问Apache官方网站(https://httpd.apache.org/)下载最新版本的Apache HTTP服务器。
或者安装UWAMP等软件来一键部署LAMP环境

2、下载和安装PHP:
Apache下尤其是在Windows环境下一定要去(https://windows.php.net/)下载,并且请下载Thread Safe版本。
否则Apache开启时会报加载php模块失败的错误。

3、安装PHP所需VC++运行库:
目前PHP7、8使用VC16即可
32位:(https://aka.ms/vs/16/release/VC_redist.x86.exe
64位:(https://aka.ms/vs/16/release/VC_redist.x64.exe

4、进入Apache的conf目录,通常在conf目录下会有一个httpd.conf文件,用文本编辑器打开它。
找到LoadModule行,确保以下模块已经被加载,注意不同的版本红字处,以及检查您下载的php中文件名是否还有带apache2_4.dll字样的文件。

PHP7:
LoadModule php7_module "你的PHP安装目录/php7apache2_4.dll"

PHP8:
LoadModule php_module "你的PHP安装目录/php8apache2_4.dll"

5、剩余配置:
在PHP的安装目录中,复制 php.ini-development 文件并将其重命名为 php.ini。
用文本编辑器打开 php.ini 文件,找到 extension_dir 行,确保它指向正确的 PHP 扩展目录。例如:

extension_dir = "ext"

找到 ;extension=gd 行(去掉前面的分号),启用 GD 扩展,这是一个常用的图像处理扩展。
启用其他你需要的 PHP 扩展。

6、重启Apache服务器:
在命令行中使用以下命令重启Apache服务器:

httpd -k restart

7、测试:
在Apache的htdocs目录下创建一个index.php文件,内容如下:

<!--?php phpinfo(); ?-->

在浏览器中访问 http://localhost/index.php,如果一切配置正确,你应该能够看到PHP的信息页面。

AJAX上传附件实时返回上传进度

在ajax调用中使用XMLHttpRequest.upload监听上传过程,progress事件,输出回调函数中的event事件

示例代码如下:

    function uploadFile() {
        var data = new FormData();
        data.append('file', $("#uploadvideo").prop("files")[0]);   //file控件

        $.ajax({
            type: "post",
            url: "UploadVideo_Upload",
            data: data,
            //async: false,//同步上传
            //cache: false,//上传文件无需缓存
            processData: false,  // 不处理数据
            contentType: false, // 不设置内容类型
            success: function (data) {
                if (data == "success") {
                    $("#a-upload").remove();
                    $("#td_upload").html("上传成功,请等待审核。");
                    $("#td_error").html("");
                } else {
                    alert(data);
                }
            },
            error: function (data) {
                alert('请求失败,请刷新页面重试。');
            },
            xhr: function () {
                var xhr = new XMLHttpRequest();
                //使用XMLHttpRequest.upload监听上传过程,注册progress事件,打印回调函数中的event事件
                xhr.upload.addEventListener('progress', function (e) {
                    console.log(e);
                    //loaded代表上传了多少
                    //total代表总数为多少
                    var progressRate = (e.loaded / e.total) * 100;

                    //通过设置进度条的宽度达到效果
                    $("#span_upload").html("正在上传中,已完成 " + progressRate.toFixed(2) + "%");
                })

                return xhr;
            }
        });
    }

C# .NET 执行bat或exe文件

代码如下:新建进程,运行cmd,参数为bat文件的内容。
注意如果是IIS部署,且批处理中需要管理员权限,则还需要将该网站的应用程序池–高级设置–标识设置为:LocalSystem

string filePath = Server.MapPath("/bat.bat");
if (File.Exists(filePath))
{
    Process process = new Process();
    var startInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",       //这里执行的是bat文件,所以要在cmd中执行
        // Use /K to have the new cmd window stay after the batch file is done
        Arguments = $"/C \"{filePath}\"",
        Verb = "runas",             //管理员身份运行
        RedirectStandardOutput = true,      //重定向输出为了前台展示cmd内容
        RedirectStandardError = true,
        UseShellExecute = false
    };
    process.StartInfo = startInfo;

    process.Start();
    process.WaitForExit();

    //获取输出结果
    string output = process.StandardOutput.ReadToEnd();
    this.txtResult.Text = "已执行。\n" + output;
}

.NET C# PDFSharp使用

// Open an existing document. Providing an unrequired password is ignored.
PdfSharp.Pdf.PdfDocument document = PdfReader.Open(pdfFilePath);
PdfSecuritySettings securitySettings = document.SecuritySettings;
// Setting one of the passwords automatically sets the security level to 
// PdfDocumentSecurityLevel.Encrypted128Bit.
//securitySettings.UserPassword = "user";
securitySettings.OwnerPassword = GGGuid.ToString();
// Don't use 40 bit encryption unless needed for compatibility reasons
//securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;
// Restrict some rights.
//除打印权限其他均关闭
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = false;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = false;
securitySettings.PermitPrint = true;

//水印
for (int i = 0; i < document.Pages.Count; i++)
{
	string watermark = "水印文字";        //水印文字
	double emSize = 42;
	XFont xFont = new XFont("黑体", emSize);
	var page = document.Pages[i];

	var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
	var size = gfx.MeasureString(watermark, xFont);
	gfx.TranslateTransform(page.Width / 2, page.Height / 2);
	gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
	gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);
	var format = new XStringFormat();
	format.Alignment = XStringAlignment.Near;
	format.LineAlignment = XLineAlignment.Near;
	XBrush brush = new XSolidBrush(XColor.FromArgb(16, 0, 0, 0));
	//循环以实现全屏水印
	for (int y = 0; y < 10; y++)
	{
		for (int x = 0; x < 10; x++)
		{
			gfx.DrawString(watermark, xFont, brush, new XPoint(x * 180 - 300, y * 120), format);
		}
	}
}

document.Save(pdfFilePath);

Javascript 判断微信内置浏览器浏览

在页面中添加如下代码,检测如果是微信打开的网页,则要求用户打开其他浏览器
div元素

<div class="weixin-tip">
<p>
<img src="/images/live_weixin.png" alt="在浏览器打开" />
</p>
</div>

sciprt代码:

    $(window).on("load", function () {
        var winHeight = $(window).height();
        function is_weixin() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                return true;
            } else {
                return false;
            }
        }
        var isWeixin = is_weixin();
        if (isWeixin) {
            $(".weixin-tip").css("height", winHeight);
            $(".weixin-tip").show();
        }
    })

样式代码:

<style type="text/css">
.weixin-tip a {
text-decoration: none;
}

.weixin-tip img {
max-width: 100%;
height: auto;
}

.weixin-tip {
display: none;
position: fixed;
left: 0;
top: 0;
bottom: 0;
background: rgba(0,0,0,0.8);
filter: alpha(opacity=80);
height: 100%;
width: 100%;
z-index: 100;
}

.weixin-tip p {
text-align: center;
margin-top: 10%;
padding: 0 5%;
}
</style>