将字符串转换为HttpPostedFileBase(Convert string to HttpPostedFileBase)

编程入门 行业动态 更新时间:2024-10-28 16:21:33
将字符串转换为HttpPostedFileBase(Convert string to HttpPostedFileBase)

我正在尝试使用MVC上传附件功能。 我实际上做上传/保存附件的方法是期待HttpPostedFileBase类型。

public virtual string Upload(HttpPostedFileBase fileName) { //Code to upload/save attachment. }

我的问题是“fileName”作为字符串从UI传递。 如何将字符串(文件路径名称)转换为我的Upload方法可以使用的内容。

提前致谢。

I am trying to do upload attachment functionality using MVC. My method that is actually doing the upload/save attachment is expecting a HttpPostedFileBase type.

public virtual string Upload(HttpPostedFileBase fileName) { //Code to upload/save attachment. }

My problem is "fileName" is being passed from the UI as a string. How can I convert the string(file path name) into something my Upload method can work with.

Thanks in advance.

最满意答案

正如其他人所提到的,你的表单应该是这样的:

<form id="form_UploadFile" action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> </form>

然后,正如你所提到的,你试图通过ajax发布,你可以使用jQuery serialize()来序列化formData,并将其推送到你的控制器。

$('#form_UploadFile').serialize();

As mentioned by others, your form should look something like this:

<form id="form_UploadFile" action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> </form>

Then as you mentioned you are trying to post via ajax, you can use jQuery serialize() to serialize the formData to be pushed to your controller.

$('#form_UploadFile').serialize();

更多推荐

Upload,attachment,upload,string,方法,电脑培训,计算机培训,IT培训"/> <meta name=

本文发布于:2023-08-03 02:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382943.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   HttpPostedFileBase   Convert   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!