创建Web服务时出错

时间:2014-11-27 16:46:31

标签: c# asp.net web-services visual-studio-2012

我制作了一个简单的asp.net网络表单,它从服务器端上传和下载文件,它完美地工作但是当我复制web方法中的所有功能来创建服务时,它会产生错误。请帮我解释一下这段代码。

WebService.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.IO;
using System.Data;
using System.Web.Util;
using System.ComponentModel;
using System.Windows;
using System.Web.UI.WebControls;
using System.Web.Services.Protocols;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Xml;
using System.Text;


 namespace MovieUploaderService.Service
 {
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService1 :   System.Web.Services.WebService
{

    [WebMethod]
    public void Page_Load(object sender, EventArgs e)
    {
        this.Form.Enctype = "multipart/form-data";
        if (!IsPostBack)
        {
            GetUploadedFiles();
        }
    }


    [WebMethod]

    public void GetUploadedFiles()
    {
        using (Database1Entities SampleDb = new Database1Entities())
        {


            DataGridView.DataSource = SampleDb.UploadFiles.ToList();

            DataGridView.DataBind();
        }
    }


    [WebMethod]
    public void DataGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DownloadFile")
        {
            int File_ID = Convert.ToInt32(e.CommandArgument.ToString());
            using (Database1Entities SampleDb = new Database1Entities())
            {
                var File = SampleDb.UploadFiles.Where(f => f.Id.Equals(File_ID)).FirstOrDefault();
                if (File != null)
                {



                    Response.ContentType = File.ContentType;
                    Response.AddHeader("content-disposition", "attachment; filename=" + File.Name);
                    Response.BinaryWrite(File.Content);
                    Response.Flush();
                    Response.End();
                }
            }
        }
    }

    [WebMethod]
    public void BtnUploadFile_Click(object sender, EventArgs e)
    {
        HttpFileCollection File_Collection = Request.Files;
        using (Database1Entities SampleDb = new Database1Entities())
        {
            foreach (string File_Uploader in File_Collection)
            {
                HttpPostedFile Posted_File = File_Collection[File_Uploader];
                if (Posted_File.ContentLength > 0)
                {
                    BinaryReader Binary_Reader = new BinaryReader(Posted_File.InputStream);
                    byte[] File_Buffer = Binary_Reader.ReadBytes(Posted_File.ContentLength);
                    SampleDb.UploadFiles.Add(new UploadFile
                    {
                        Name = Posted_File.FileName,
                        ContentType = Posted_File.ContentType,
                        Extension = Path.GetExtension(Posted_File.FileName),
                        Size = Posted_File.ContentLength,
                        Content = File_Buffer
                    });
                }
            }
            SampleDb.SaveChanges();
        }
        GetUploadedFiles();
    }

     }
   }

问题在于post和response ..虽然网格视图也会出错

客户端aspx文件代码是

ASPX

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"               CodeBehind="Sample.aspx.cs" Inherits="WebApplication8.Sample" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
   <script src="Scripts/jquery-1.10.2.js"></script>
  <div class="form-horizontal">
    <h4>Uploading Multiple Files</h4>
    <hr />
    <asp:ValidationSummary runat="server" CssClass="text-danger" />
    <div class="form-group">
        <asp:Label runat="server" CssClass="col-md-2 control-label">Choose File</asp:Label>
        <div class="col-md-10" id="MultipleFileUploader">
            <p>
                <asp:FileUpload runat="server" ID="FileUploader" CssClass="form-control" />
                <a href="#" id="AddAnotherUploader">Add Files</a>
            </p>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <asp:Button runat="server" ID="BtnUploadFile" OnClick="BtnUploadFile_Click" 
            Text="Upload Files" CssClass="btn btn-default" />
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" CssClass="col-md-2 control-label">Select Files:</asp:Label>
        <div class="col-md-10">
            <asp:GridView runat="server" ID="DataGridView" AutoGenerateColumns="false"  OnRowCommand="DataGridView_RowCommand" CssClass="form-control">
                <Columns>
                    <asp:BoundField HeaderText="File Name" DataField="Name" />
                    <asp:BoundField HeaderText="File Size" DataField="Size" />
                    <asp:TemplateField HeaderText="Get File">
                        <ItemTemplate>
                            <asp:LinkButton ID="LbnDownload" runat="server"       CommandName="DownloadFile" 
                                CommandArgument='<%# Eval("Id") %>'>Download</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
     </div>
  </div>
</asp:Content>

我是网络服务课程的初学者,请帮助我... :(

3 个答案:

答案 0 :(得分:1)

不要将控件事件添加到WebService.asmx只需将方法写入WebService文件并在包含控件事件的.aspx页面中调用它们

答案 1 :(得分:0)

告诉我们得到了哪个错误会很有用。

但实际上,Web服务不应该与ASPX端一起使用。服务器端发布一些可以通过客户端的HTTP调用访问和执行的方法。例如,可以使用代理从客户端到服务器进行这些调用(例如,可以通过在Visual Studio中添加Web引用来创建客户端上的代理)。没有什么可以处理GUI(即ASPX)。

此外,我不认为网络服务是将电影上传到服务器的最佳方式。 FTP对于大文件更好。我认为你将通过电影打破网络服务配额。

答案 2 :(得分:0)

Web服务和网站完全不同。因此,请勿将您网站上的所有代码复制到网络服务中。这样做的理想方法是提出全新的Web服务项目。您可以通过参考这些文章找到Web服务和Web应用程序之间的不同。 in herehere