How to insert data along with file using wcf

Posted by Mandlaa under WCF on 4/17/2014 | Points: 10 | Views : 1263 | Status : [Member] | Replies : 2
I want to insert data along with file using wcf,
Data store in database and file store in Application folder,
Give me some examples




Responses

Posted by: Mandlaa on: 4/19/2014 [Member] Starter | Points: 25

Up
0
Down
Once see my code

ISoftwareR

public interface ISoftwareR
{
[OperationContract]
[WebInvoke(UriTemplate = "/RequestSoftware", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
int InsertData(Request obj, RemoteFileInfo request);
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;

[MessageHeader(MustUnderstand = true)]
public long Length;

[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;

public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}


SoftwareR

public int InsertData(Request obj, RemoteFileInfo request)
{
//File
FileStream targetStream = null;
Stream sourceStream = request.FileByteStream;

string uploadFolder = @"C:\upload\";

string filePath = Path.Combine(uploadFolder, request.FileName);

using (targetStream = new FileStream(filePath, FileMode.Create,
FileAccess.Write, FileShare.None))
{
//read from the input stream in 65000 byte chunks

const int bufferLen = 65000;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
{
// save to output stream
targetStream.Write(buffer, 0, count);
}
targetStream.Close();
sourceStream.Close();
}
//End
int result = 0;
SoftDATA.EDaaSDataContext ctx = new SoftDATA.EDaaSDataContext();
Request obj2 = new Request();
obj2.RequestID = obj.RequestID;
obj2.CreatedDate = DateTime.Now;

try
{
ctx.Request.Add(obj2);
//ctx.Request(obj2);
//ctx.AddToRequest(obj2); // saving Employee data
ctx.SaveChanges();

}
catch (Exception ex)
{
result = 0;
}

return result;
}
Request class:

[Table("Request")]
public class Request
{
public Guid RequestID { get; set; }
public string Name { get; set; }
public string RequestType { get; set; }
public Guid? RequestGlobalID { get; set; }
public string RequestExternalID { get; set; }
public string RequestorID { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime? DeploymentDate { get; set; }
public DateTime? ExpirationDate { get; set; }
public string Progress { get; set; }
public string Status { get; set; }
public string ActivityName { get; set; }
public string DatacenterLocationID { get; set; }
public int? ApplicationCatalogID { get; set; }
public int? ApplicationInstanceID { get; set; }
public string ServerName { get; set; }
public string ServerFQDN { get; set; }
public string ServerIPv4 { get; set; }
public int? ServerID { get; set; }
public string ServerPrimaryApp { get; set; }
public string ServerDRClassification { get; set; }
public string ServerDataClassification { get; set; }
public int? ServerCPU { get; set; }
public int? ServerRAM { get; set; }
public Single? ServerOSBuild { get; set; }
public string ServerOSEdition { get; set; }
public string ServerArchitecture { get; set; }
public int? ServerDriveC { get; set; }
public int? ServerDriveE { get; set; }
public int? ServerDriveF { get; set; }
public int? ServerDriveG { get; set; }
public string ServerEnvironment { get; set; }
public string ServerZone { get; set; }
public string ServerPrimaryRole { get; set; }
public string ServerADDomain { get; set; }
public string ServerLocalAdmins { get; set; }
public string RequestorEmail { get; set; }
public string CentralLocationID { get; set; }
public string CentralApproval { get; set; }
public string DataCenterApproval { get; set; }
public string RequestTypeHW { get; set; }
public int? ApplicationEnvironmentID { get; set; }

}
EDaaSDataContext

public class EDaaSDataContext : DbContext
{
public EDaaSDataContext()
: base("name=EDaaSDataContext")
{
}
public DbSet<Request> Request { get; set; }
}
Onece see my code and Modify my code,

i AM using the WCF Data Services? This is different then WCF Services.

what won't work:

string uploadFolder = @"C:\upload\";

hOW CAN I DO THIS PLEASE MODIFY MY CODE

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 4/21/2014 [Member] Starter | Points: 25

Up
0
Down
public string InsertData(SoftwareRequestViewModel softwareRequestViewModel,HttpPostedFileBase instanceTemplate)
{
if (instanceTemplate != null)
{
var fileName = Path.GetFileName(instanceTemplate.FileName);
instanceTemplate.SaveAs(Server.MapPath("~/InstanceTemplate/") + fileName);
var dt = new DataTable();
var path = Server.MapPath("~/InstanceTemplate/" + fileName);
dt = ExcelFile.Read(instanceTemplate, 0, 0, path, Constants.RequestMode.Software);
if (dt.Rows.Count != 0)
{
var uploadInstanceTemplateViewModel = FilterDataFromTheReturnedDatatable(dt, softwareRequestViewModel.ApplicationCatalogID);
var applicationInstance = new ApplicationInstance
{
ApplicationCatalogID = uploadInstanceTemplateViewModel.ApplicationCatalogId,
ConfigFileContent = uploadInstanceTemplateViewModel.ConfigFileContent,
ConfigFileDestination = uploadInstanceTemplateViewModel.ConfigFileDestination,
ConfigFileName = uploadInstanceTemplateViewModel.ConfigFileName,
Name = uploadInstanceTemplateViewModel.InstanceName
};
_softwareRequestRepository.SaveApplicationInstance(applicationInstance);
var serverName = softwareRequestViewModel.ServerNameFqdn;
string[] splittedServerName = serverName.Split('.');
var request = new Request
{
RequestID = Guid.NewGuid(),
Name = softwareRequestViewModel.RequestName,
RequestorID = softwareRequestViewModel.RequestorName,
DatacenterLocationID = softwareRequestViewModel.DataCenterLocationId,
RequestorEmail = softwareRequestViewModel.RequestorEmail,
ServerName = splittedServerName[0],
RequestType = Constants.RequestMode.Software.ToString(),
ExpirationDate = DateTime.Now.AddDays(2),
Progress = Constants.Progress.Queued.ToString(),
Status = Constants.Status.Success.ToString(),
ApplicationCatalogID = softwareRequestViewModel.ApplicationCatalogID,
ApplicationInstanceID = applicationInstance.ApplicationInstanceID,
ServerFQDN = softwareRequestViewModel.ServerNameFqdn,
CreatedDate = DateTime.Now,
ModifiedDate = DateTime.Now,
DeploymentDate = DateTime.Now,
ServerCPU = 0,
ServerRAM = 0,
ServerOSBuild = 0,
CentralApproval = "False",
DataCenterApproval = "False",
ServerArchitecture = "64-bit",
ServerDriveC = 0,
ServerDriveE = 0,
ServerDriveF = 0,
ServerDriveG = 0,
RequestTypeHW = "Provision",
ApplicationEnvironmentID = 0

};
_softwareRequestRepository.SaveSoftwareRequest(request);
BindDropDown();
return "success";
}
return "Please choose a valid file!!";
BindDropDown();

}
return "Please choose a file!!";
BindDropDown();

}

I am writting this code for this i am getting one error below line

var path = Server.MapPath("~/InstanceTemplate/" + fileName);

the server does not exist in current context

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response