Reputation: 51
i made a simple asp.net web form which uploads and downloads file from server side it worked perfectly but when i copied all the functions in web methods to create a service it gives errors. kindly please help me with this code.
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();
}
}
}
The problem is with post and response .. While the grid view also makes error
the client side aspx file code is
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>
i am a beginner to web services course please help me with this.. :(
Upvotes: 0
Views: 90
Reputation: 117
Web service and a web site is totally different thing. therefore don't copy all the codes from your website to web services. The ideal way to do that is come up with brand new web service project. you can find the different between web service and web application by referring to these articles. in here and here
Upvotes: 0
Reputation: 384
Dont add your control events to WebService.asmx just write methods to WebService files and call them in your .aspx page containing control event
Upvotes: 1
Reputation: 56
It would be usefull to tell which error we got.
But actually, a webservice is not supposed to be used with an ASPX side. The server side publishes some methods that can be accessed and executed through HTTP calls by clients. These calls can be made from a client to the server with a proxy for example (the proxy on the client can simply be created by adding a web reference with Visual Studio for example). There is nothing to deal with GUI (i.e. ASPX).
Furthermore, I don't think a webservice is the best way to upload movies to a server. FTP is better for big files. I think you'll break web service quota with movies.
Upvotes: 0