使用form post(php)将文件上载到Azure存储,并返回链接

使用form post(php)将文件上载到Azure存储,并返回链接,php,jquery,ajax,azure,Php,Jquery,Ajax,Azure,我在这里和其他地方读过许多解决方案,它们展示了如何将文件上传到Azure Blob存储。当文件是硬编码的(遵循MS教程)时,下面的php代码可以工作,但如果我试图将文件发布到php中,则不会工作(获取错误:不是有效路径)。我希望用户能够浏览表单中的文件并提交(然后返回url,以便我可以将其用于其他操作)。我继续阅读,以便javascript显示一个虚假的安全路径,我认为这是我的问题。那么,如果我不能发布PHP文件,如何获得正确的路径呢。我尝试了一个默认帖子和一个ajax尝试。有解决办法吗 My

我在这里和其他地方读过许多解决方案,它们展示了如何将文件上传到Azure Blob存储。当文件是硬编码的(遵循MS教程)时,下面的php代码可以工作,但如果我试图将文件发布到php中,则不会工作(获取错误:不是有效路径)。我希望用户能够浏览表单中的文件并提交(然后返回url,以便我可以将其用于其他操作)。我继续阅读,以便javascript显示一个虚假的安全路径,我认为这是我的问题。那么,如果我不能发布PHP文件,如何获得正确的路径呢。我尝试了一个默认帖子和一个ajax尝试。有解决办法吗

My PHP(文件硬编码时工作):

require_once'WindowsAzure\WindowsAzure.php';
使用WindowsAzure\Common\ServicesBuilder;
使用WindowsAzure\Common\ServiceException;
$connectionString=“DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey”;
//创建blob REST代理。
$blobRestProxy=ServicesBuilder::getInstance()->createBlobService($connectionString);
//$content=fopen(“c:\myfile.txt”、“r”)//当像这样硬编码时,这是有效的
//$blob_name=“myblob.txt”;
//发帖
//$fpath=$\u POST[“resFile”]//我也试过了-不行
$fpath=$\u文件[“resFile”];
$fname=“hello.txt”;
$content=fopen($fpath,“r”)//我知道这是不对的,但是试着
$blob_name=$fname;
试一试{
//上传blob
$blobRestProxy->createBlockBlob(“saskcontainer”,$blob_name,$content);
}
捕获(ServiceException$e){
//根据错误代码和消息处理异常。
//错误代码和消息如下:
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
$code=$e->getCode();
$error_message=$e->getMessage();
echo$code.“:”$error_message.“
”; } //成功后,我需要在这里返回url
以下是HTML和JS:

 <form action="test.php" method="post" enctype="multipart/form-data">
 <div data-role='page' id="resFileCont" >

    <input type="file" name="resFile" id="resFile" value="" />
    <!--<input type="text" name="name" />-->
    <input type="submit" value="Submit" data-inline="true"/>
</div></form>
<div id="res"></div>
<script>
    $(document).ready(function () {
        $("form").on('submit', (function (e) {
            e.preventDefault();

            $.ajax({
                url: "test.php", // Url to which the request is send
                type: "POST",             // Type of request to be send, called as method
                data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
                contentType: false,       // The content type used when sending data to the server.
                cache: false,             // To unable request pages to be cached
                processData: false,        // To send DOMDocument or non processed data file it is set to false
                success: function (data)   // A function to be called if request succeeds
                {
                    alert(data)
                    $("#res").html(data)
                }
            });
        }));
    });
</script>

$(文档).ready(函数(){
$(“表格”)。在(‘提交’,(功能(e){
e、 预防默认值();
$.ajax({
url:“test.php”,//请求发送到的url
类型:“POST”,//要发送的请求类型,称为方法
数据:newformdata(this),//发送到服务器的数据,一组键/值对(即表单字段和值)
contentType:false,//向服务器发送数据时使用的内容类型。
cache:false,//无法请求缓存页面
processData:false,//若要发送DOMDocument或未处理的数据文件,将其设置为false
success:function(data)//请求成功时要调用的函数
{
警报(数据)
$(“#res”).html(数据)
}
});
}));
});
刚刚更换
$content=fopen($fpath,“r”)
具有

$content=fopen($_文件[“resFile”][“tmp_名称”],“r”)

明白了。工具太长的简单解决方案:
$content=fopen($\u FILES[“resFile”][“tmp\u name”],“r”)您应该添加您的评论作为答案,并接受它,因为它适合您:)
 <form action="test.php" method="post" enctype="multipart/form-data">
 <div data-role='page' id="resFileCont" >

    <input type="file" name="resFile" id="resFile" value="" />
    <!--<input type="text" name="name" />-->
    <input type="submit" value="Submit" data-inline="true"/>
</div></form>
<div id="res"></div>
<script>
    $(document).ready(function () {
        $("form").on('submit', (function (e) {
            e.preventDefault();

            $.ajax({
                url: "test.php", // Url to which the request is send
                type: "POST",             // Type of request to be send, called as method
                data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
                contentType: false,       // The content type used when sending data to the server.
                cache: false,             // To unable request pages to be cached
                processData: false,        // To send DOMDocument or non processed data file it is set to false
                success: function (data)   // A function to be called if request succeeds
                {
                    alert(data)
                    $("#res").html(data)
                }
            });
        }));
    });
</script>