闪闪
闪闪
  • 发布:2014-09-01 09:12
  • 更新:2014-12-08 16:02
  • 阅读:4076

plus.uploader.addFile与addData添加的内容在服务端接口怎么接收

分类:HBuilder

.net中,context.Request.Files里是空的。
context.Request.InputStream得到了addFile与addData添加的内容,但是怎么区分出来?
或者有其他的什么办法得到上传的图片?

2014-09-01 09:12 3 条评论 负责人:无 分享
已邀请:
DCloud_App_Array

DCloud_App_Array

文件上传使用的标准http协议,提交数据为multipart/form-data格式(包含key:value),提交方式为POST
对于.net开发我们没有深入研究过,应该是通过context.Request.InputStream或context.Request.InputStream.Form来读取,具体操作方式还请到搜索相关教程。

如果是php,可以用$_FILES进行访问,简单例子如下:

<?php  

if ($_SERVER['REQUEST_METHOD'] == 'POST') {  

    $ret=array('strings'=>$_POST,'error'=>'0');  

    $fs=array();  

    foreach ( $_FILES as $name=>$file ) {  

        $fn=$file['name'];  
        $ft=strrpos($fn,'.',0);  
        $fm=substr($fn,0,$ft);  
        $fe=substr($fn,$ft);  
        $fp='files/'.$fn;  
        $fi=1;  
        while( file_exists($fp) ) {  
            $fn=$fm.'['.$fi.']'.$fe;  
            $fp='files/'.$fn;  
            $fi++;  
        }  

        move_uploaded_file($file['tmp_name'],$fp);  

        $fs[$name]=array('name'=>$fn,'url'=>$fp,'type'=>$file['type'],'size'=>$file['size'],);  
    }  

    $ret['files']=$fs;  

    echo json_encode($ret);  
}else{  
    echo "{'error':'Unsupport GET request!'}";  
}  

?>
chenqp

chenqp

context.Request.Files是有文件的

  • 闪闪 (作者)

    有的,我后来测试看到有。不知道开始为什么没有。。。

    2014-11-07 16:58

aaaa

aaaa

请问.net的接收图片成功了吗?我也想知道

lintg

lintg

.NET不行的

lintg

lintg

普通上传没问题,但是uploader的格式太特殊,header就和别的上传不一样

  • aaaa

    普通上传用的哪个?加我QQ1046373779,我不是很明白,想请教您

    2014-12-08 15:58

lintg

lintg

普通上传很简单啊,一般的webform进来的数据是标准的 multipart/form-data格式,比如处理phonegap上传的文件xxx.ashx,

public class uploadfile : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
var host = "http://"+context.Request.ServerVariables["HTTP_HOST"];
var basePath = "../upload";
var Value1 = context.Request["value1"];//这里就是获取得到phonegap上传的传的参数
var Value2 = context.Request["value2"];//这里就是获取得到phonegap上传的传的参数
var ht = new Hashtable();

  if (context.Request.Files.Count > 0)  
  {  

//取到文件对象
HttpPostedFile file = context.Request.Files[0];
//取得文件后缀名(带个点)
string ext = System.IO.Path.GetExtension(file.FileName);
Random ran = new Random();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 1000) + ext;
。。。。

if (!Directory.Exists(savePath2))
Directory.CreateDirectory(savePath2);
savePath += "/"+ fileName;
file.SaveAs(savePath);
....

主要代码都有了

  • aaaa

    哦哦,好吧,谢谢啦

    2014-12-08 16:08

  • aaaa

    那您图片上传后来怎么解决的

    2014-12-08 16:10

该问题目前已经被锁定, 无法添加新回复