Fileupload控件始终返回Hasfile属性为false

时间:2018-06-25 04:07:02

标签: html asp.net

我正在尝试上载文件,但是上载控件始终返回false,由于下面的代码我的代码不起作用。我正在提交按钮事件中将文件存储在数据库中。谁能告诉我我要去哪里错了。

谢谢

<div class="col-md-4">
  <asp:Label ID="lblPhoto" runat="server" Text="Photo"></asp:Label>
  <asp:FileUpload ID="fileupload" CssClass="form-control" runat="server" />
</div>

    if (fileupload.HasFile)
  {
     HttpPostedFile postedfile = fileupload.PostedFile;
     string filename = Path.GetFileName(postedfile.FileName);
    string fileextension = Path.GetExtension(filename);
    if (fileextension.ToLower() == ".jpg" || fileextension.ToLower() == ".bmp" || fileextension.ToLower() == ".gif" || fileextension.ToLower() == ".png")
       {

        Stream stream = fileupload.postedfile.InputStream;
        BinaryReader binaryreader = new BinaryReader(stream);
        photodata = binaryreader.ReadBytes((Int32)stream.Length);

       }
       else
         {
        LblRegisterStatus.Text = "Only files with(.jpg .bmp .gif .png) can be uploaded";
        LblRegisterStatus.ForeColor = System.Drawing.Color.Red;
          }
      }

2 个答案:

答案 0 :(得分:2)

您也可以通过jquery使用文件上传

JS函数

function SaveFiles() {

    var fileInput = document.getElementById('fileInput');
    var file = $("#fileInput").val();

    if (file.length > 0) {
        var fd = new FormData();
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/Admin/UploadImage');
        xhr.setRequestHeader('Content-type', 'multipart/form-data');

        //Appending file information in Http headers
        xhr.setRequestHeader('X-File-Name', fileInput.files[0].name);
        xhr.setRequestHeader('X-File-Type', fileInput.files[0].type);
        xhr.setRequestHeader('X-File-Size', fileInput.files[0].size);
        xhr.setRequestHeader['X-File-FileName', 1];
        xhr.setRequestHeader['X-File-Id', id];

        //Sending file in XMLHttpRequest
        xhr.send(fileInput.files[0]);
        xhr.onreadystatechange = function (data) {

            if (xhr.readyState == 4 && xhr.status == 200) {

                alert("Image Uploaded Successfully...");

            }
        }
    }
}

**C# Code**

public JsonResult UploadImage()
        {
            string fileName = Request.Headers["X-File-Name"];
            string fileType = Request.Headers["X-File-Type"];
            int fileSize = Convert.ToInt32(Request.Headers["X-File-Size"]);

            System.IO.Stream fileContent = Request.InputStream;
            System.IO.FileStream fileStream = System.IO.File.Create(Server.MapPath("~/UploadImg/" + fileName));
            fileContent.Seek(0, System.IO.SeekOrigin.Begin);

            //Copying file's content to FileStream
            fileContent.CopyTo(fileStream);
            fileStream.Dispose();

            string FileName = Server.MapPath("~/UploadImg/" + fileName);

            //Here you can code for insert in database

            return Json(FileName);
        }

答案 1 :(得分:0)

FileUpload控件与UpdatePanel不兼容。您有两个选择

  1. 将控件移到UpdatePanel之外。

  2. 如果不可能,请在UpdatePanel上添加一个PostBackTrigger 一个例子 FileUpload控件与UpdatePanel不兼容。您有两个选择

将控件移到UpdatePanel之外 如果不可能,请在UpdatePanel上添加PostBackTrigger 一个例子

<Triggers>
   <asp:PostBackTrigger ControlID="yourButtonIdThatSubmitsFile" />
</Triggers>