上传pdf文件的网页服务


Post web service to upload pdf file

我在php中制作了一个脚本,从iOS应用程序上传pdf文件到服务器上,但我不明白为什么它说未定义索引pdfFile请让我知道,如果你抓住我的错误

这是我的iOS代码

 Alamofire.upload(
        multipartFormData: { multipartFormData in
          //  multipartFormData.append(pdfUrl!, withName: "pdfFile")
            let pdfData = NSData(contentsOf: pdfUrl!)
            print((pdfData as? Data)!)
            multipartFormData.append((pdfData as? Data)!, withName: "pdfFile", mimeType: "application/pdf")

        },
        to: "http://www.webservice.pixsterstudio.com/uploadpdf.php",
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, , ):
                upload.responseJSON { response in
                    debugPrint(response)
                    print(response.result)
                }
            case .failure(let encodingError):
                print(encodingError)
            }
        }
    )
这是我的PHP web服务脚本:- uploadpdf.php
  <?php
if ($_FILES['pdfFile']['type'] == "application/pdf") {
    $source_file = $_FILES['pdfFile']['tmp_name'];
    $dest_file = "webservice.pixsterstudio.com/upload/".$_FILES['pdfFile']['name'];
    if (file_exists($dest_file)) {
        print "The file name already exists!!";
    }
    else {
        move_uploaded_file( $source_file, $dest_file )
        or die ("Error!!");
        if($_FILES['pdfFile']['error'] == 0) {
            $Return['status'] = 'true';
            $Return['message'] = "Pdf file uploaded successfully!";
            //print "Pdf file uploaded successfully!";
            //print "<b><u>Details : </u></b><br/>";
            //print "File Name : ".$_FILES['pdfFile']['name']."<br.>"."<br/>";
        //  print "File Size : ".$_FILES['pdfFile']['size']." bytes"."<br/>";
        //  print "File location : upload/".$_FILES['pdfFile']['name']."<br/>";

        }
    }
}
else {
    if ( $_FILES['pdfFile']['type'] != "application/pdf") {
            $Return['status'] = 'false';
            $Return['message'] = "Pdf file not uploaded !";
        //print "Error occured while uploading file : ".$_FILES['pdfFile']['name']."<br/>";
        //print "Invalid  file extension, should be pdf !!"."<br/>";
        //print "Error Code : ".$_FILES['pdfFile']['error']."<br/>";
    }
}
  header('Content-type: application/json');
  echo  json_encode($Return);
 ?>

当我在等咖啡的时候,我很快写了这篇文章——这是我通常处理文件上传的方式——希望它可能有用。

$fieldname = 'pdfFile';
$targetdir = 'webservice.pixsterstudio.com/upload/';
$desiredtype = 'application/pdf';
$return=array(
    'status'    =>    'false',
    'message'    =>    'Pdf file not uploaded!'
);
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES[ $fieldname ] ) ){
    try{
        $obj = (object)$_FILES[ $fieldname ];
        $name = $obj->name;
        $size = $obj->size;
        $tmp  = $obj->tmp_name;
        $type = $obj->type;
        $error= $obj->error;


        if( is_uploaded_file( $tmp ) && $error == UPLOAD_ERR_OK && $type == $desiredtype ){
            $destination = $targetdir . $name;
            if( file_exists( $destination ) ){
                $return['status']='false';
                $return['message']='File already exists!';
                $return['line']=__LINE__;
                clearstatcache();
            } else {
                $res = move_uploaded_file( $tmp, $destination );
                $return['status']=$res ? 'true' : false;
                $return['message']=$res ? 'Pdf file uploaded successfully!' : 'Moving the file failed!';
            }
        } else {
            $return['status']='false';
            $return['message']='File upload error!';
            $return['line']=__LINE__;
        }
    }catch( Exception $e ){
            $return['status']='false';
            $return['message']=$e->getMessage();
            $return['line']=__LINE__;
    }
    header('Content-type: application/json');
    exit( json_encode( $return ) );
} else {
    exit( header( 'HTTP/1.1 405 Method Not Allowed',true, 405 ) );
}

使用以下表单,我现在收到一个json消息,说上传失败…

    <form method='post' action='http://www.webservice.pixsterstudio.com/uploadpdf.php' enctype="multipart/form-data">
        <h1>Upload PDF to WebService</h1>
        <input type='file' name='pdfFile' />
        <input type="submit" value="Send">
    </form> 
{"status":"false","message":"Pdf file not uploaded!"}

让我们检查一下您的代码错误文件mime类型。检查文件类型在PHP中采取mimetype文件上传

每个上传文件的mime类型都不同

if ($_FILES['pdfFile']['type'] == "pdf") {
}

替换你的PHP文件类型检查行,见上文。此代码检查每个PDF mime类型文件上传。

下面是我的ios代码这是我的代码,我没有得到任何类型的错误。

func callPostWithMultipartService(urlString: String, param : [String: AnyObject]?, fileData : NSData,  completion: (result: String, data : AnyObject) -> Void)
{
    upload(.POST, urlString, multipartFormData: { MultipartFormData in
        MultipartFormData.appendBodyPart(data: fileData, name: "filepdf", fileName: "file.pdf", mimeType: "application/pdf")
        for (key, value) in param! {
            MultipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
        }