您的位置:首页 > 编程语言 > C语言/C++

c++客户端文件上传到c#ashx

2015-09-22 16:43 429 查看
c++端:

AnsiString sendFile(String url,String path,String fl){

String rt ="error";
TIdMultiPartFormDataStream *stream = new TIdMultiPartFormDataStream();
TIdHTTP *IdHTTP1 = new TIdHTTP(NULL);

try
{
stream->AddFormField("dir",path);
stream->AddFile("FiledName",fl,"multipart/form-data");
stream->Position = 0;
IdHTTP1->Request->ContentType = "multipart/form-data";
rt=IdHTTP1->Post(url,stream) ;

}
__finally
{
stream->Free();
IdHTTP1->Free();
}
return rt;
}

调用:

if (opendialog1->Execute()) {
  sendFile(hosturl+"absupload.ashx",gpath,opendialog1->FileName);
}

c#端

        public void ProcessRequest(HttpContext context)
        {
            String ff = context.Request["dir"];
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
            HttpFileCollection hfc = context.Request.Files;
            HttpPostedFile upfile = hfc[0];
            String fl = upfile.FileName;
            int i1 = fl.LastIndexOf("\\") + 1;
            int i2 = fl.Length;
            string root = System.Web.Configuration.WebConfigurationManager.AppSettings["fileroot"];
            String path = context.Server.MapPath("\\") + ff;
            if (!Directory.Exists(path))//   判断是否存在        
            {
                Directory.CreateDirectory(path);//  创建新路径       
            }
            fl = ff+"\\"+fl.Substring(i1, i2-i1);

            upfile.SaveAs(context.Server.MapPath(fl));
            //upfile.SaveAs("e:\\"+fl);
            if (hfc[0].InputStream != null) hfc[0].InputStream.Close();
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: