简体   繁体   English

无法使用C#从asp.net的客户端接收文件

[英]Can't receive files from client side in asp.net using C#

I'm trying to get a profile image from a form. 我正在尝试从表单获取个人资料图像。 The html looks like this: html看起来像这样:

<form action="UpdateProfile.aspx" method="post">
  <input type='file' name='profilePic'/>
</form>

The server side code looks like this: 服务器端代码如下所示:

HttpPostedFile image = Request.Files["profilePic"];
string imagePath = @"../Pictures/Profile/DefaultPicture.png";
if (image.ContentLength >0 && image != null)
{
   if (Path.GetExtension(image.FileName) == "jpg" || Path.GetExtension(image.FileName) == "jpeg" ||
       Path.GetExtension(image.FileName) == "png" || Path.GetExtension(image.FileName) == "gif" ||
       Path.GetExtension(image.FileName) == "bmp")
   {
       string fileExtention = Path.GetExtension(image.FileName);
       string FileName = Session["Username"].ToString() + "." + fileExtention;
       image.SaveAs(Server.MapPath(Path.Combine("../Pictures/Profile/", FileName)));
       imagePath = Path.Combine("../Pictures/Profile/", FileName);
   }
}

But it never works, no matter what I upload the image variable is always null . 但是它永远都行不通,无论我上传什么image变量始终为null its like the server cant recive it or something. 就像服务器无法接收它一样。 can you guys help me solve it or tell me where i went wrong? 你们可以帮助我解决问题或告诉我哪里出问题了吗? Thank you in advance. 先感谢您。

Be sure that you set the Content-Type correctly: 确保正确设置Content-Type:

The file collection is populated only when the HTTP request Content-Type value is "multipart/form-data". 仅当HTTP请求的Content-Type值为“ multipart / form-data”时,才会填充文件集合。

https://msdn.microsoft.com/en-us/library/system.web.httprequest.files%28v=vs.110%29.aspx https://msdn.microsoft.com/zh-cn/library/system.web.httprequest.files%28v=vs.110%29.aspx

Here is an example: 这是一个例子:

<FORM action="..."
       enctype="multipart/form-data"
       method="post">
   ...
</FORM>

An example to guide you. 指导您的示例。

 <asp:FileUpload ID="FileUpload1" runat="server" />
 <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />

 using System.IO;
 using System.Collections.Generic;
 protected void Upload(object sender, EventArgs e)
 {
    if (FileUpload1.HasFile)
    {
    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
    Response.Redirect(Request.Url.AbsoluteUri);
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何接收和保存从客户端(使用Ajax进行J查询)发送到服务器端(ASP.net C#)的数据? - How to receive and save the data sent from client side (J-query using Ajax) to server side (ASP.net C#)? 无法使用 ajax 将数据从 asp.net 网络表单传递到客户端 - Can't pass data from asp.net webforms to client side using ajax 从服务器端ASP.NET C#访问从客户端创建的缓存变量 - Accessing Cache Variable created from client side from the Server Side ASP.NET C# 客户端和服务器端的Asp.net C#加密/解密 - Asp.net C# Encryption/Decryption on Client and server Side 在 ASP.NET C# 中在客户端打印 Crystal Report - Printing Crystal Report on Client Side in ASP.NET C# 客户端C#在asp.net中出错 - Client side C# gives error in asp.net 读取ASP.NET C#中的客户端串行端口 - Read Client side Serial Port in ASP.NET C# 如何使用C#或ASP.net从客户端调用私有void按键事件处理程序,以仅允许数字进入文本框 - How to call a private void keypress event handler from client side using C# or ASP.net to allow only numbers into textbox 如何在客户端C#(asp.net)中检查Caps Lock状态 - How can i check Caps Lock state in C# (asp.net), client side 从ASP.NET在客户端运行C#Interop Control ActiveX - Running C# Interop Control ActiveX on Client Side from ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM