在表单数据中使用参数?
Using parameters in form data?
如何获取我在网上写的方法中使用formData发送的数据API。这里我只能获取到图片文件名
我的组件 >>
我在formdata中的参数:
1- 图片文件
2-siparisid
3-dokumantipi
4- aciklama
@ViewChild('ufile') ufile: ElementRef;
fileUploadKaydet() {
const formData = new FormData();
let headers = new Headers();
formData.append('ufile', this.ufile.nativeElement.files[0]);
formData.append('siparisid', this.siparis.siparisid);
formData.append('dokumantipi', this.form.controls.belgeTipiFormController.value);
formData.append('aciklama', this.form.controls.aciklamaFormController.value);
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:64478/api/dokumanlar/ResimKaydet', formData, options)
.map(res => res.json()).subscribe(
data => console.log('success'),
error => console.log(error)
)
}
Web Api >>
HttpFileCollection files = HttpContext.Current.Request.Files 代码行;我只能获取图像文件。如何获取通过 FormData 发送的参数?例如,如何获得 "siparisid"
public class SiparisDokumanController : ApiController
{
[HttpPost]
[AllowAnonymous]
[Route("ResimKaydet")]
public async Task<IHttpActionResult> ResimKaydet()
{
int uploadCount = 0;
string sPath = HostingEnvironment.MapPath("");
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string fileName = new FileInfo(file.FileName).Name;
if (file.ContentLength > 0)
{
string modifiedFileName =
Guid.NewGuid().ToString().Replace("-", "") + "_" + fileName;
if (!File.Exists(sPath + Path.GetFileName(modifiedFileName)))
{
file.SaveAs(sPath + Path.GetFileName(modifiedFileName));
uploadCount++;
//Context.Galeri.Add(new Galeri() { Id=id, FileName =
"/Galeri/"+modifiedFileName, Title=fileName });
}
}
}
if (uploadCount > 0)
{
// db ye kaydedecek kod
//Context.SaveChange();
//return Ok("kayıt başarılı");
}
return Ok("Kayıt Başarılı");
}
}
如何读取最后保存的图像文件?例如,我找不到一行代码。我需要用 web 编写此代码 api 2. 感谢您的帮助
我研究过。它正在被这样处理。
HttpFileCollection files = HttpContext.Current.Request.Files;
string _siparisid = HttpContext.Current.Request["siparisid"];
string _dokumanTipi = HttpContext.Current.Request["dokumantipi"];
string _aciklama = HttpContext.Current.Request["aciklama"];
如何获取我在网上写的方法中使用formData发送的数据API。这里我只能获取到图片文件名
我的组件 >>
我在formdata中的参数:
1- 图片文件
2-siparisid
3-dokumantipi
4- aciklama
@ViewChild('ufile') ufile: ElementRef;
fileUploadKaydet() {
const formData = new FormData();
let headers = new Headers();
formData.append('ufile', this.ufile.nativeElement.files[0]);
formData.append('siparisid', this.siparis.siparisid);
formData.append('dokumantipi', this.form.controls.belgeTipiFormController.value);
formData.append('aciklama', this.form.controls.aciklamaFormController.value);
let options = new RequestOptions({ headers: headers });
this.http.post('http://localhost:64478/api/dokumanlar/ResimKaydet', formData, options)
.map(res => res.json()).subscribe(
data => console.log('success'),
error => console.log(error)
)
}
Web Api >>
HttpFileCollection files = HttpContext.Current.Request.Files 代码行;我只能获取图像文件。如何获取通过 FormData 发送的参数?例如,如何获得 "siparisid"
public class SiparisDokumanController : ApiController
{
[HttpPost]
[AllowAnonymous]
[Route("ResimKaydet")]
public async Task<IHttpActionResult> ResimKaydet()
{
int uploadCount = 0;
string sPath = HostingEnvironment.MapPath("");
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string fileName = new FileInfo(file.FileName).Name;
if (file.ContentLength > 0)
{
string modifiedFileName =
Guid.NewGuid().ToString().Replace("-", "") + "_" + fileName;
if (!File.Exists(sPath + Path.GetFileName(modifiedFileName)))
{
file.SaveAs(sPath + Path.GetFileName(modifiedFileName));
uploadCount++;
//Context.Galeri.Add(new Galeri() { Id=id, FileName =
"/Galeri/"+modifiedFileName, Title=fileName });
}
}
}
if (uploadCount > 0)
{
// db ye kaydedecek kod
//Context.SaveChange();
//return Ok("kayıt başarılı");
}
return Ok("Kayıt Başarılı");
}
}
如何读取最后保存的图像文件?例如,我找不到一行代码。我需要用 web 编写此代码 api 2. 感谢您的帮助
我研究过。它正在被这样处理。
HttpFileCollection files = HttpContext.Current.Request.Files;
string _siparisid = HttpContext.Current.Request["siparisid"];
string _dokumanTipi = HttpContext.Current.Request["dokumantipi"];
string _aciklama = HttpContext.Current.Request["aciklama"];