【发布时间】:2019-12-14 20:10:44
【问题描述】:
我正在尝试在 c# 中使用 web api 上传文件。我试过邮递员。它工作正常。我很困惑如何在 c# 代码中做到这一点。 我尝试了以下代码,但它给出了错误。
var request = (HttpWebRequest)WebRequest.Create("http://Api_projects/add_project");
var postData = "name=thisIsDemoName&img=" + Server.MapPath(FileUpload1.FileName) +"&info=ThisIsDemoInfo";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "multipart/form-data";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Response.Write(responseString);
当运行代码时,它会在屏幕上显示以下错误消息
遇到 PHP 错误 严重性:通知 消息:未定义索引:img
文件名:controllers/Api_projects.php
行号:27
回溯:
文件:/home/fpipj1blp4wo/public_html/ecosense.in/application/controllers/Api_projects.php 线路:27 函数:_error_handler
文件:/home/fpipj1blp4wo/public_html/ecosense.in/application/libraries/REST_Controller.php 线路:785 函数:call_user_func_array
文件:/home/fpipj1blp4wo/public_html/ecosense.in/index.php 线路:315 函数:require_once
请帮忙
【问题讨论】:
-
Server.MapPath 根据服务器的本地文件系统给出路径 - 这可能不是可公开访问的路径。另外,如果该文件正在上传,它可能还不存在
-
这个错误是由我们一无所知的
controllers/Api_projects.php报告的