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();
}
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();
}
相关文章推荐
- C++隐式转换
- 深入解析C++中类的多重继承
- Effective C++——条款52(第8章)
- C++设计模式——抽象工厂模式
- C++替换字符
- MD5算法的C++实现
- vector简单应用
- 简单介绍C++编程中派生类的析构函数
- C语言第二节-关系运算符,switch,if
- C语言第一节-进制,运算符,输入输出
- 解析C++中多层派生时的构造函数及一些特殊形式
- c++中try catch的用法
- hdu1695GCD(莫比乌斯反演初步)
- Visual Studio 2015专业版创建Win32控制台应用程序,C,C++源文件
- C++设计模式——观察者模式
- C++多线程Singleton模式
- 高质量C++/C编程指南-第7章-内存管理(3)
- C++设计模式——单例模式
- 深入解析C++中派生类的构造函数
- 实用工具——ACM工程代码.cpp/.java化