I have json file generated from my database in the following format
[{"UserInfoID":"57adb1e8-27ab-e311-afb2-eca86bf5549a","DbType":"OFFLINE","UserID":"d3f3b6a6-77aa-436a-a73c-d6655566888b","GraphID":"56adb1e8-27ab-e311-afb2-eca86bf5549a","UserLevelEnumId":"00000000-0000-0000-0000-000000000000","Title":"Mr/Mrs","FirstName":"Neeraj","MiddleName":null,"LastName":"Mehta","Birthdate":"\/Date(699388200000)\/","Gender":"Male","Email":"neer.srmiet#gmail.com","MobileNo":"+918950333123","Country":"India","Zipcode":133201,"UserLevel":999,"CountFollowers":0,"CountFollows":0,"CountFiles":0,"CountPhotos":0,"Quote":"Empowering Education","AvatarURL":"/UserFiles/Images/X50/IMAG1289_1vv4djTgspjjsl87fquality_50.jpg","PosterURL":null,"isVerified":true,"VerificationCount":0,"UserEnumType":"USER","UserCreatorId":"d3f3b6a6-77aa-436a-a73c-d6655566888b"}
and i have downloaded this data in a file and now i want to upload this data in other database having same structure and same attribute
How can i upload this from a file and suggestion?
Create a class that match this object if you don't have it yet (lets call this class 'user').
If you want to upload this json data using ajax create the following action:
[HttpPost]
public ActionResult(User user) {
// Add input validation here.
// Add the user entity to the database.
UsersContext context = new UsersContext();
context.Users.Add(user);
context.SaveChanges();
}
Related
I have a scenario in MVC where I am uploading Excel files using OPEN XML to read spreadsheets, I however do not want to save these records to the database yet, as I perform validation against the records and if validation succeeds I then save the record to the database, this is for performance reasons as I can have 1000's of records in the excel spreadsheet, I was able to edit well if I first save the records to the database, I would simply read them from db by Id , modify and then update/save the changes. I don't want to do this, I need to enable my app to read the the records in memory edit, re-validate then save only valid data, how can I do this in MVC, please see my current code below which uses the approach of saving the records first which is what I want to avoid.
public async Task<ActionResult> Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
CleanSupplierClaim cleanData = await db.CleanSupplierClaims.FindAsync(id);
if (cleanData == null)
{
return HttpNotFound();
}
return View(cleanData);
}
// POST: /Claim/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit([Bind(Include = "Id,Action,Line_Number,Total_Claim,Currency,ClaimReference,ST_Key,Warning,Error_1,Error_2,Error,Domain_Username")] CleanSupplierClaim cleanData)
{
if (ModelState.IsValid)
{
if (cleanData.Id == 0)
{
db.CleanSupplierClaims.Add(cleanData);
}
else
{
db.Entry(cleanData).State = System.Data.Entity.EntityState.Modified;
}
await db.SaveChangesAsync();
//return PartialView();
}
return RedirectToAction("RedirectToValidateClaimsView");
}
Thank you in advance
HTTP is stateless. You have to write data somewhere.
"Somewhere" could be:
In page data (hidden form fields, JavaScript variables, etc.)
In cookies (not very suitable for large amounts of data)
In session state
In static variables (probably not good in a web application for a couple of reasons, but an option nonetheless)
In a file
In a separate database (maybe a local SQL database file for just that application, or maybe a document database, etc.)
In the main database but in separate tables (tables meant to hold "temporary" data which hasn't been committed to the "real" tables yet)
etc.
The point is, you have to write data somewhere. It doesn't have to be your main database tables. But if different requests to a web application need to operate on the same persistent data then that data needs to be persistent somewhere.
If you want to validate your Excel files:
Make a new class called Validation
Inside this class create a methode where you can validate your excel files.
Return true or false for valid or invalid.
For each excel file call this method and if it is valid, add it to a list with all your other valid excel files.
Save all valid excel files in your database
As far as I do not know your environment nor your validation criterias, it's hard for me to provide you any code. But maybe the list above can help you.
A client of mine came with the following demand:
Add the option to upload a file to the server
Save the file id as a guid in database
I followed this answer- so the file upload is working(to a specific location on the server)
http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0
What i don't familiar with is his second demand:
Save the file id as a guid in database
What does it mean?
Why does the file need a guid id in the database?
What should I do in extra to the post I linked to?(security check, extension check?)
guid = globaly unique identifier.
Why does a file need a guid in database ? well because its your client's requirement its not required by the database.
And finally how to do it? is simple when you will be saving a filename to database generate a guid and save it to the database you can generate a guid like this Guid id = Guid.NewGuid(); and you can create a separate column in the database for saving file's guids.
I'm creating a web application using MVC 5 and Identity. I have so far created a registration system, but I would like to allow users to upload a profile when they register.
I was wondering if it is possible to implement profile pictures with asp.net identity?
Yes. Assuming you're using the default Entity Framework implementation, you can extend the ApplicationUser in a file called Models/IdentityModels.cs.
You can store the image in the database or elsewhere on the file system.
One way to store it in the database is using a byte array in the model (which I believe maps to varbinary(max))...
public class ApplicationUser : IdentityUser
{
public byte[] Image { get; set; }
}
Details on how to save upload an image and save in the database via EF can be found here...
Entity Framework 5 Code first adding an image
Or you can simply save the uploaded file to the file system or to blob storage, then store the path or URL to the image...
public class ApplicationUser : IdentityUser
{
public string ImagePath { get; set; }
}
Best thing todo is to create a second table (or add a column to the current table), add a file upload feature to the registration form. When registration is successful add the picture to the db with EntityFramework. Create a page to return the picture by the userid so you can include it in a page somewhere.
Something like this http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx but with a profile picture
I have a functional app using breeze. Now I need to be able to export CSV files with the data. What would be nice to be able to do is to use the breeze client-side query system to get the data. Eg.: Today I already have a method in my controller:
public IQueryable<Movie> Movies()
{
return _context.Context.Movies;
}
and on client side I can create filters, etc. Is it possible to do something so I can do something like
public string ExportMovies()
{
var movies = _context.Context.Movies;
//movies holds my filtered list
var csvFile = movies.ToCSV("path")
//return the path to the user download
return path;
}
My biggest problem is that I want to re-utilize breeze client-side query capabilities instead of creating one of my one. Since breeze expetc to get Entity data I do not see how or if this is doable... is it possible?
First part of question:
I have information in DB and I want to get it from db and save it as .txt file to client.
I have done it with Regular asp.net. but in mvc not yet. My information is not an image. This information about peoples
I watched to This site
Second part of question:
I want to download file to client. There is not problem when downloading one file, but I want to download 3 file at once time with 1 request. But it could not be done. So I decided to create zip file and generate link. When user will click to link it will download to user.
What you think? Is it good to do it with this way?
Third part of question:(new)
How i can delete old .zip files from catalog after succes download? or another way. Lets say with service which will run on server.
You could have the following controller action which will fetch the information from the database and write it to the Response stream allowing the client to download it:
public ActionResult Download()
{
string info = Repository.GetInfoFromDatabase();
byte[] data = Encoding.UTF8.GetBytes(info);
return File(data, "text/plain", "foo.txt");
}
and in your view provide a link to this action:
<%= Html.ActionLink("Downoad file", "Download") %>
You can delete the temporary file returned by using an Action Filter like this. Then apply the attribute to your MVC action method.
public class DeleteTempFileResultFilter : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string fileName = ((FilePathResult)filterContext.Result).FileName;
filterContext.HttpContext.Response.Flush();
filterContext.HttpContext.Response.End();
System.IO.File.Delete(fileName);
}
}